我知道有很多类似的问题,而且我已经阅读了其中的大部分问题,并且它们与其他问题相关。 我有这个错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:1, request=2, result=-1, data=Intent { dat=content://com.google.android.gallery3d.provider/picasa/item/5953655552200168930 flg=0x1 }} to activity {std.app/std.app.activity.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
问题是它可以在某些设备上运行,而在某些设备上则不能。例如,它适用于 SGS2,但不适用于 Nexus 7。 更多日志:
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at std.app.util.PhotoUtils.getRealPathFromURI(PhotoUtils.java:124)
at std.app.fragments.DayFragment.onActivityResult(DayFragment.java:167)
at android.app.Activity.dispatchActivityResult(Activity.java:5427)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
这是出错的代码:
public static String getRealPathFromURI(Context context, Uri contentURI) {
String result;
Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx); // this line
cursor.close();
}
return result;
}
不同版本的MediaStore.Images.ImageColumns.DATA
是否不同?如何解决这个问题?
编辑: 我发现这不是版本的问题,而是源码的问题。仅当我从 Picassa 文件夹中选取图片时才会出现此错误。
最佳答案
这是因为云同步照片(又名 Picassa 文件夹)在 SD 卡或内存中没有物理地址,它不是设备上的物理文件。 MediaStore.Images.ImageColumns.DATA
列用于设备上的物理文件。
“抓取”此图像的方法是打开一个 Stream 并将图像复制到您控制的文件夹(应用程序中的临时文件夹,可能来自 Context.getCacheDir()
方法) .
InputStream is = context.getContentResolver().openInputStream(uri);
OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile));
然后继续从输入复制到输出并在最后关闭流。 请记住,此过程是一个网络操作,这意味着您永远不应该从 UI 线程执行此操作。
如果您使用像 Picasso
这样的图像库(您应该始终使用它),您可以让它为您完成所有工作,只需传递 Uri
像这样直接指向它 Picasso.with(context).load(uri).into(target);
这个目标可以是 ImageView
或任何 实现目标
关于android - 在 Android 中从 uri 查询时无法从光标窗口读取 row 0, col -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800030/