zoukankan      html  css  js  c++  java
  • Context.managedQuery()和context.getContentResolver()获取Cursor关闭注意事项

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

    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();
    }
  • 相关阅读:
    Linux下安装vmtools的语句
    [WP]BugkuCtf
    Linux文件属性及权限
    学习pwn的前提工作及部分解决方案
    windows环境下MySQL mysql-5.7.17-winx64 (社区服务版,community server)安装教程
    ubuntu14.04 LTS 更新国内网易163源
    session cookie
    java collection map
    重温 总结 maven几个重要概念
    java通信
  • 原文地址:https://www.cnblogs.com/rwxwsblog/p/5646447.html
Copyright © 2011-2022 走看看