0

In my App the user can pick an image from the gallery:

private void pickPicture() {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    this.startActivityForResult(intent, Globals.REQUEST_PICK_PHOTO);
}

With the above code my gallery gets opened up and it shows me all my fotos but it is definetely not the old gallery i had on my phone (im using google nexus 4 with the newest android version 5.1.1). the new gallery looks like the following:

enter image description here

However when i now click on an image the app crashes:

07-29 18:30:41.896    2693-2693/de.mypackage.myproject E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns.
07-29 18:30:41.896    2693-2693/de.mypackage.myproject D/AndroidRuntime﹕ Shutting down VM
07-29 18:30:41.898    2693-2693/de.mypackage.myproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.mypackage.myproject, PID: 2693
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/10961/ACTUAL/102077033 flg=0x1 (has clip) }} to activity {de.mypackage.myproject/de.mypackage.ProfileEditPictureActivity}: 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 de.mypackage.utils.Helper.getRealPathFromURI(Helper.java:1064)
            at de.mypackage.fragments.profile.picture.ProfilePictureEditFragment.onActivityResult(ProfilePictureEditFragment.java:109)
            at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
            at de.mypackage.activities.ProfileEditPictureActivity.onActivityResult(ProfileEditPictureActivity.java:51)
            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)

My onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK) {
        if(requestCode == Globals.REQUEST_TAKE_PHOTO) {
            this.startCropImage();
        } else if (requestCode == Globals.REQUEST_PICK_PHOTO) {
            this.imageFile = new File(Helper.getRealPathFromURI(this.getActivity(), data.getData()));

            this.startCropImage();
        } else if (requestCode == Globals.REQUEST_CROP_PHOTO) {
            this.imageView.setImageBitmap(Images.loadBitmapFromFile(Paths.IMAGE_PROFILE));

            String message = this.getResources().getString(R.string.text_profile_save_completed);

            Toast.makeText(this.getActivity(), message, Toast.LENGTH_LONG).show();
        }
    }
}

And now the code where the app crashes:

public static String getRealPathFromURI(Context context, Uri contentURI) {
    String result = null;
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);

    if (cursor == null) {
        result = contentURI.getPath();
    } else { 
        cursor.moveToFirst(); 

        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 

        result = cursor.getString(index);
        cursor.close();
    }

    return result;
}

The app crashes exactly at:

result = cursor.getString(index)

The code i posted above works on all android versions older than 5.1.1 but not on 5.1.1 so there need to be something different. Do you know what the problem is?

Mulgard
  • 9,877
  • 34
  • 129
  • 232

3 Answers3

2

The code i posted above works on all android versions older than 5.1.1

Only on the couple of devices that you tried, and only for the photos that you tried.

A Uri is not a file. You cannot get a "real path" for images, because there is no requirement that the Uri point to a file that you can access. For example, your code will not work for >400 million devices (all that shipped with Android 4.4+) for photos that are on removable media, because you do not have read access to arbitrary locations on removable media.

Use the Uri with a ContentResolver to get at the bytes represented by that Uri and the MIME type, much as you would use something like HttpUrlConnection to get the bytes and MIME type for a URL.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So i guess the answer from Paul Bruke here: http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework will solve it. – Mulgard Jul 29 '15 at 17:04
  • @Mulgard: No. For example, your app will still be broken on >400 million devices (all that shipped with Android 4.4+) for photos that are on removable media, because you do not have read access to arbitrary locations on removable media. The way to "solve it" is to do what I wrote, and use `ContentResolver` to access the content identified by the `Uri`. – CommonsWare Jul 29 '15 at 17:23
  • I thought this is it. I cant find any other solutions with ContentResolver in the web. – Mulgard Jul 29 '15 at 17:24
  • oh is it simple as that? `getContentResolver().openInputStream(uri)` but that means i need to create a temp file for each image because im working with files only. – Mulgard Jul 29 '15 at 17:26
  • @Mulgard: Ideally, you fix the rest of your code to not work with "files only". – CommonsWare Jul 29 '15 at 17:28
  • I guess you are right. I could use `BitmapFactory` to create a `Bitmap` from the `InputStream`. Let's get to work. thx. – Mulgard Jul 29 '15 at 17:30
  • @CommonsWare, i noticed you posted this similar answer as a comment on another question. Please provider a code sample for us that arnt well versed in android.(i checked the link to your website aswell) – BiGGZ Apr 07 '17 at 02:23
1

This error occurs when there no such column that you are fetching. To make sure just print out your cursor by this

System.out.println(DatabaseUtils.dumpCursorToString(yourcursor));

and see if there is such column or not.

Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
0

try with this method, and see this question

public String getImagePath(Uri uri){
   Cursor cursor = getContentResolver().query(uri, null, null, null, null);
   cursor.moveToFirst();
   String document_id = cursor.getString(0);
   document_id = document_id.substring(document_id.lastIndexOf(":")+1);
   cursor.close();

   cursor = getContentResolver().query( 
   android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
   null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
   cursor.moveToFirst();
   String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
   cursor.close();

   return path;
}
Community
  • 1
  • 1
David Hackro
  • 3,652
  • 6
  • 41
  • 61