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

发布于 2024-11-19 22:04:22 字数 2596 浏览 1 评论 0原文

当我运行以下代码时,我收到 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();

这是堆栈跟踪

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

I am getting a IllegalStateException when I run the following code:

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

Here is the 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)

The error is at:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-11-26 22:04:22

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

我相信它的 MediaStore.MediaColumns.SIZE 。

Include SIZE in your query projection(the second param in the content resolvers query method).

its MediaStore.MediaColumns.SIZE i believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文