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:
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?