查询MediaStrore.Images时出现非法状态异常

时间:2011-07-13 06:04:46

标签: android android-contentprovider

运行以下代码时,我收到IllegalStateException:

Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null);
                        imageCursor.moveToFirst();
                        String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                        Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));
                        Toast.makeText(getApplicationContext(), String.valueOf(imageSize), Toast.LENGTH_LONG).show();

这是stacktrace

07-12 10:49:27.426: ERROR/AndroidRuntime(1397): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.database.CursorWindow.getLong_native(Native Method)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.database.CursorWindow.getLong(CursorWindow.java:380)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.database.CursorWrapper.getLong(CursorWrapper.java:127)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at com.messageHider.viewImageThumb$1.onClick(viewImageThumb.java:93)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.os.Looper.loop(Looper.java:123)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at java.lang.reflect.Method.invoke(Method.java:521)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-12 10:49:27.426: ERROR/AndroidRuntime(1397):     at dalvik.system.NativeStart.main(Native Method)

错误发生在:

Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));

1 个答案:

答案 0 :(得分:0)

在查询投影中包含SIZE(内容解析器查询方法中的第二个参数)。

我相信它的MediaStore.MediaColumns.SIZE。