0

This code used to work fine with the Nexus 5 image gallery. Ever since moving to the newer Google Photos app selecting images crash this code.

Error:

08-07 10:10:10.792 25086-25086/com.app.app E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns. 08-07 10:10:10.795 25086-25086/com.app.app D/AndroidRuntime﹕ Shutting down VM 08-07 10:10:10.801 25086-25086/com.app.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.app.app, PID: 25086 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/17870/ACTUAL/1285012603 flg=0x1 (has clip) }} to activity {com.app.app/com.app.app.PhotoActivity}: 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.app.ActivityThread.deliverResults(ActivityThread.java:3574) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) at android.app.ActivityThread.access$1300(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 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:438) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) at android.database.CursorWrapper.getString(CursorWrapper.java:114) at com.app.app.PhotoActivity.getRealPathFromURI(PhotoActivity.java:456) at com.app.app.PhotoActivity.onActivityResult(PhotoActivity.java:436) at android.app.Activity.dispatchActivityResult(Activity.java:6192) at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)             at android.app.ActivityThread.access$1300(ActivityThread.java:151)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5254)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

ONACTIVITYRESULT:

case ACTION_SELECT_PHOTO: {
    if (resultCode == RESULT_OK) {
        Uri selectedImageURI = data.getData();
        File imageFile = new File(getRealPathFromURI(selectedImageURI)); // LINE 436
        mCurrentPhotoPath = imageFile.toString();
        if (mCurrentPhotoPath != null) {
            setPic();
            mCurrentPhotoPath = null;
        }
    }
    break;
}

GETREALPATHFROMURI:

private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = 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); // HERE IS THE ERROR: LINE 456
        cursor.close();
    }
    return result;
}

I'd guess my issue is in one of two places, either getContentResolver().query needs to use some newer method. OR the getColumnIndex(MediaStore.Images.ImageColumns.DATA) MediaStore is the incorrect way to deal with the newer cloud based photo apps.

1 Answer 1

1

A URI does not mean there is a file. An app is able to provide the contents through any mechanism it wants (a local file, a local file in a private directory you can't read, streamed over the internet, built dynamically on the fly).

Instead, you should use ContentResolver.openInputStream(contentUri) to get the contents from a URI. You can then transform that into a Bitmap via BitmapFactory.decodeStream()

4
  • Should I do the ContentResolver.openInputStream(contentUri) in my onActivityResult? Aug 20, 2015 at 15:05
  • Yes, you can use it right after your Uri selectedImageURI = data.getData() line. Aug 20, 2015 at 15:11
  • This is random, but do you know how to create a new bitmap from an existing while setting options as well? Before updating to this code I used Bitmap bitmap = BitmapFactory.decodeFile(f, bmOptions); the main option sent is inSampleSize for scaling purposes. I've looked into createScaledBitmap but that just uses height and width. Aug 21, 2015 at 1:36
  • inSampleSize is only available with the decodeZZZ() methods because it does not require reading the whole Bitmap into memory. If you already have a Bitmap, you already have it in memory and you can use createScaledBitmap() with the height/width divided by 2, 4, etc to accomplish the same thing. Aug 21, 2015 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.