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

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. Java并发编程核心方法与框架-exchanger的使用

    Exchanger可以使两个线程之间传输数据,比生产者/消费者模式使用wait/notify更加方便. Exchanger中的exchange()方法具有阻塞的特点,此方法被调用后等待其他线程来取数据 ...

  2. Spring4学习笔记-AOP

    1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...

  3. JavaScript 五种(构造方式)继承

    一.对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(this.user ...

  4. Redis3.0.7 cluster/集群 安装配置教程

    1.前言 环境:CentOS-6.7-i386-LiveDVD 安装的CentOs系统 节点: 6个节点,3个主节点.3个从节点(由于redis默认需要3个主节点,如果想每个主节点有一个从节点,这是最 ...

  5. shell编程中for file in $*; do是什么意思.

    $*是此行命令所在函数(脚本)的所有被传入参数的合集与$@类似,不用引号的情况下没有区别区别是当被""扩起来以后"$*"被当做一个字符串"$@&quo ...

  6. php后台多用户权限组思路与实现程序代码

    网站开发少不了有网站后台,有了后台自然要对用户有同角色来分配一下,特别是多用户系统的情况下,如我一个系统要有多个管理员,那么我这些管理要分成,编辑,友情连接,管理员等,那我们要有权限和角色分配,今天我 ...

  7. Linux下查看文件内容的命令

    查看文件内容的命令: cat     由第一行开始显示内容,并将所有内容输出 tac     从最后一行倒序显示内容,并将所有内容输出 more    根据窗口大小,一页一页的现实文件内容 less ...

  8. Linux统计文件行数

    语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...

  9. xcode 插件

    http://www.cocoachina.com/ios/20160122/15080.html https://github.com/rickytan/RTImageAssets http://m ...

  10. C++之map、list操作

    #include <iostream> #include "map_struct.h" #include <map> using namespace std ...