在获取图片缩略图时,获取游标并进行相关的操作。

Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
THUMBNAIL_STORE_IMAGE,
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?",
new String[]{id + ""},
null);

  出现如下异常:

07-06 09:40:14.945 26649-26671/com.xxx.xxx E/ContentResolver: Cursor leak detected, query params: uri=content://media/external/images/thumbnails, projection=[Ljava.lang.String;@4214bca8, selection=image_id = ?, selectionArgs=[Ljava.lang.String;@421d05f8, sortOrder=null
07-06 09:40:14.945 26649-26671/com.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: Thread-1075
java.lang.IllegalStateException: Process 26649 exceeded cursor quota 100, will kill it
at android.os.Parcel.readException(Parcel.java:1433)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
at android.content.ContentResolver.query(ContentResolver.java:411)
at android.content.ContentResolver.query(ContentResolver.java:354)
at com.xxx.xxx.utils.localalbum.common.LocalImageHelper.getThumbnail(LocalImageHelper.java:179)
at com.xxx.xxx.utils.localalbum.common.LocalImageHelper.initImage(LocalImageHelper.java:139)
at com.xxx.xxx.utils.localalbum.common.LocalImageHelper$1.run(LocalImageHelper.java:92)
at java.lang.Thread.run(Thread.java:856)

  这是由于游标没有关闭导致的问题。在finnally里面对cursor进行关闭即可。

Cursor cursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
THUMBNAIL_STORE_IMAGE,
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?",
new String[]{id + ""},
null);
try{
...
}finally {
if(null != cursor){
cursor.close();
}
}

  然而,如果使用Context.managedQuery(),如果在android4.0以上,如果使用了Cursor.close()方法;则会报如下异常:

E/AndroidRuntime(31184): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.

  在android 4.0及其以上的版本中,Cursor会自动关闭,不需要用户自己关闭。

  解决办法:

if(VERSION.SDK_INT < 14) {
cursor.close();
}

Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项的更多相关文章

  1. Android Context完全解析与各种获取Context方法

    Context类型 我们知道,Android应用都是使用Java语言来编写的,那么大家可以思考一下,一个Android程序和一个Java程序,他们最大的区别在哪里?划分界限又是什么呢?其实简单点分析, ...

  2. Android 缓存目录 Context.getExternalFilesDir()和Context.getExternalCacheDir()方法

    一.基础知识 应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载 ...

  3. Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别

    <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过packagesanning的方式)上面的注解 ...

  4. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  5. EF How to use context.Set and context.Entry, which ships with EF4.1 ?

    How to use context.Set and context.Entry, which ships with EF4.1 ? Hello, I am trying to implement a ...

  6. Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别

    <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...

  7. context:exclude-filter 与 context:include-filter 转

    context:exclude-filter 与 context:include-filter 转 1 在主容器中(applicationContext.xml),将Controller的注解打消掉 ...

  8. [ERROR][org.springframework.web.context.ContextLoader][main] Context initialization failed org.sprin

    做一个SSH为基础框架的webapp小DEMO,复制了一把以前可以跑的代码,竟发现无法初始化数据源,报错如下: [ERROR][org.springframework.web.context.Cont ...

  9. <context:annotation-config> 和 <context:component-scan>的差别

    <context:annotation-config> is used to activate annotations in beans already registered in the ...

随机推荐

  1. Yii2 redis与cache

    原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...

  2. verilog阻塞与非阻塞的初步理解(一)

    通过两个模块来区别两者.测试平台:Modelsim altera 6.5b 阻塞模块: module blocking(clk,a,b,c); :] a; input clk; :] b,c; :] ...

  3. 360chrome,google chrome浏览器使用jquery.ajax加载本地html文件

    使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...

  4. mysql数据库存储过程异常处理

    14.1.4  定义条件和处理程序 定义条件和处理程序是事先定义程序执行过程中可能遇到的问题.并且可以在处理程序中定义解决这些问题的办法.这种方式可以提前预测可能出现的问题, 并提出解决办法.这样可以 ...

  5. Promise 异步(asynchronous )编程

    概述 Promise.all(iterable) 方法返回一个promise,该promise会等iterable参数内的所有promise都被resolve后被resolve,或以第一个promis ...

  6. motto2

    Baby you've done enough that cut your breath.Don't beat yourself up don't need to turn so fast.Somet ...

  7. 弹出框四 之toastr.js (完成提示框)

    1.下载 toastr.js组件 2. $(function () { toastr.success('提交数据成功'); toastr.error('Error'); toastr.warning( ...

  8. Python程序的常见错误(收集篇)

    关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...

  9. PHP利用Curl实现多线程抓取网页和下载文件

    PHP 利用 Curl  可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,然而因为php语言本身不支持多线程,所以开发爬虫程序效率并不高,一般采集 数据可以利用 PHPquery ...

  10. 微信或移动端网页的meta

    针对微信: <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> ...