mediastore - 在游标查询( 图像/视频光标) 中,Android 应用 崩溃

  显示原文与译文双语对照的内容
93 1

我的Android 应用 在以下代码行崩溃:


Cursor cursor= getActivity().getContentResolver().query(
 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,columns,
 selection, selectionArgs, orderBy);

崩溃日志:


08-26 13:15:19.307 6992 6992 E AndroidRuntime: FATAL EXCEPTION: main
08-26 13:15:19.307 6992 6992 E AndroidRuntime: android.database.CursorWindowAllocationException: Cursor window could not be created from binder.
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.CursorWindow.<init>(CursorWindow.java:133)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.CursorWindow.<init>(CursorWindow.java:41)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:681)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:679)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.BulkCursorDescriptor.readFromParcel(BulkCursorDescriptor.java:75)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.BulkCursorDescriptor$1.createFromParcel(BulkCursorDescriptor.java:34)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.database.BulkCursorDescriptor$1.createFromParcel(BulkCursorDescriptor.java:30)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.content.ContentProviderProxy.query(ContentProviderNative.java:369)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:372)
08-26 13:15:19.307 6992 6992 E AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:315)

我如何避免它?

我怀疑下面的代码可能是导致崩溃的原因


if (cursor!= null)
 cursor.moveToFirst();

游标始终关闭。

任何帮助提前致谢。

谢谢

时间: 原作者:

117 3

试试这个


Cursor cursor= getApplicationContext().getContentResolver().query(
 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,columns,
 selection, selectionArgs, orderBy);

 if (cursor!= null) {
 if (cursor.moveToFirst()) {
/////Put your idea here
 }
 cursor.close(); 
}

原作者:
...