在Android 5.1.1上从外部存储中挑选图像时应用程序崩溃

时间:2015-07-29 16:43:48

标签: android

在我的应用中,用户可以从图库中选择图像:

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);
}

通过上面的代码,我的画廊被打开了,它向我展示了我的所有照片,但它绝对不是我手机上的旧画廊(我使用谷歌nexus 4与最新的Android版本5.1.1)。新画廊如下所示:

enter image description here

然而,当我现在点击图片时,应用程序崩溃了:

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)

我的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();
        }
    }
}

现在应用程序崩溃的代码:

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;
}

该应用程序完全崩溃:

result = cursor.getString(index)

我上面发布的代码适用于所有早于5.1.1的Android版本,但不适用于5.1.1,因此需要有不同的东西。你知道问题是什么吗?

3 个答案:

答案 0 :(得分:2)

  

我上面发布的代码适用于所有早于5.1.1的Android版本

仅适用于您尝试过的几台设备,仅适用于您尝试过的照片。

A Uri is not a file。您无法获得图像的“真实路径”,因为不要求Uri指向您可以访问的文件。例如,对于可移动媒体上的照片,您的代码不适用于> 4亿台设备(Android 4.4+附带的所有设备),因为您无法对可移动媒体上的任意位置进行读取访问。

使用UriContentResolver来获取Uri所代表的字节数和MIME类型,就像使用HttpUrlConnection之类的内容来获取URL的字节和MIME类型。

答案 1 :(得分:1)

如果没有要提取的列,则会发生此错误。 确保只用这个

打印光标
System.out.println(DatabaseUtils.dumpCursorToString(yourcursor));

并查看是否有这样的专栏。

答案 2 :(得分:0)

尝试使用此方法,并查看此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;
}