Reputation: 5635
I know there is a lot similiar question, and I have read most of them and they relate to something else. I have this error :
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.
The problem is it works on some devices, and on some not. For example it works on SGS2, but not on Nexus 7. More logs :
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)
and here is the code which makes error :
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;
}
Is the MediaStore.Images.ImageColumns.DATA
different in different versions? How to fix this?
EDIT : I have noticed that this is not the problem of version, but the problem of source. This error shows only when I pick a picture from Picassa folders.
Upvotes: 2
Views: 830
Reputation: 39856
that's because cloud synced Photos (a.k.a. Picassa folder) do not have a physical address in the SD card or internal memory, it is not a physical file on the device. The column MediaStore.Images.ImageColumns.DATA
is for physical files on the device.
The way to "grab" this image is to open an Stream and copy the image over to a folder you control (a temporary folder in your app, maybe from Context.getCacheDir()
method).
InputStream is = context.getContentResolver().openInputStream(uri);
OutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile));
and then proceeded with copying from the input to the output and close the streams at the end. Remember that this process is a network operation, that means you should never do it from the UI thread.
If you're using an image library like Picasso
(which you should always use) you can just let it do all the work for you and just pass the Uri
directly to it like this Picasso.with(context).load(uri).into(target);
this target can either be an ImageView
or any object that implements Target
Upvotes: 3