您的位置:首页 > 移动开发 > Android开发

android.database.StaleDataException: Attempted to access a cursor after it has been clos

2014-06-11 10:19 330 查看
今天在开发选择图片时发现有时候会报错:

Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.

手机是android版本是4.2.1

网上查找了一下发现是由于android版本的问题,4.0以上会自动关闭游标,不需要手动关闭,故再进行手动关闭的时候就报出了以上异常

修改前的代码:

<span style="font-size:18px;">    String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null,null);
String picPath=null;
if(cursor != null )
{
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
cursor.close();
}  </span>


<span style="font-size:18px;">	        String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null,null);
String picPath=null;
if(cursor != null )
{
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
try
{
//4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
if(Integer.parseInt(Build.VERSION.SDK) < 14)
{
//只有4.0以下才需要手动关闭
cursor.close();
}
}catch(Exception e)
{
CommonHelper.log(TAG+":addPhoto",e.getMessage());
}
}
</span>
这样上述问题就解决了,有时候版本不同确实会带来很多问题,开发过程中还是要小心为妙
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐