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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM