Android 无法从下载文件夹中选择图像

Android 无法从下载文件夹中选择图像,android,cursor,Android,Cursor,我想从手机中选择一个图像,并将其显示在ImageView中。当我从图库中选择图像时,它可以正常工作。但是,当我从手机的下载文件夹中选择图像时,它会消失: 08-04 18:45:17.394: E/AndroidRuntime(31777): Process: com.app.myapp, PID: 31777 08-04 18:45:17.394: E/AndroidRuntime(31777): java.lang.RuntimeException: Failure delivering r

我想从手机中选择一个图像,并将其显示在ImageView中。当我从图库中选择图像时,它可以正常工作。但是,当我从手机的下载文件夹中选择图像时,它会消失:

08-04 18:45:17.394: E/AndroidRuntime(31777): Process: com.app.myapp, PID: 31777
08-04 18:45:17.394: E/AndroidRuntime(31777): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content: flg=0x1 }} to activity {com.app.myapp/com.app.myapp.MyActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
08-04 18:51:19.194: E/AndroidRuntime(32386): 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.
08-04 18:51:19.194: E/AndroidRuntime(32386):    at android.database.CursorWindow.nativeGetString(Native Method)
08-04 18:51:19.194: E/AndroidRuntime(32386):    at android.database.CursorWindow.getString(CursorWindow.java:492)
08-04 18:51:19.194: E/AndroidRuntime(32386):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-04 18:51:19.194: E/AndroidRuntime(32386):    at android.database.CursorWrapper.getString(CursorWrapper.java:118)
08-04 18:51:19.194: E/AndroidRuntime(32386):    at com.app.myapp.MyActivity.getRealPathFromURI(MyActivity.java:582)
08-04 18:51:19.194: E/AndroidRuntime(32386):    at com.app.myapp.MyActivity.onActivityResult(MyActivity.java:517)
这是我获取图像路径的方式:

selectedImageUri = data.getData();
filename = getRealPathFromURI(selectedImageUri);
iv_photo.setImageURI(selectedImageUri);
以及功能:

 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); //force close here
            cursor.close();
        }
        return result;
    }
我有一种预感,上面的代码指的是储存在图库中的图像,下载文件夹也有所不同。
我能够从SD卡的根目录添加图像,因此它必须是下载文件夹…

我成功地做到了这一点,因为事实证明KITKAT中的情况发生了变化。我就是这样做的:

   case LOAD_FROM_GALLERY_COMPLETE:  //before KitKat    
        if (data != null) {
            complete_dialog.selectedImageUri = data.getData();
            Log.i("URI", String.valueOf(complete_dialog.selectedImageUri));
            complete_dialog.filename = getRealPathFromURI(complete_dialog.selectedImageUri);
            complete_dialog.iv_bucket2.setImageURI(complete_dialog.selectedImageUri);
        }
        break;
   case LOAD_FROM_GALLERY_KITKAT_COMPLETE:  //after KitKat
        if (data != null) {
            complete_dialog.selectedImageUri = data.getData();
            Log.i("URI_KITKAT", String.valueOf(complete_dialog.selectedImageUri));
            complete_dialog.filename = getPath(BucketProfileActivity.this, complete_dialog.selectedImageUri);
            complete_dialog.iv_bucket2.setImageURI(complete_dialog.selectedImageUri);
        }
    break;

private String getRealPathFromURI(Uri contentURI) {
             String result;
             Cursor cursor = getContentResolver().query(contentURI, null, "", null, null);
             if (cursor == null) { // Source is Dropbox or other similar local file path
                 result = contentURI.getPath();
             } else { 
                 cursor.moveToFirst(); 
                 int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
                 Log.i("IDX", idx + "");
                 Log.i("RESULT", cursor.getString(idx) + "");
                 result = cursor.getString(idx); //force close here
                 cursor.close();
             }
             return result;
         }


 @TargetApi(Build.VERSION_CODES.KITKAT) @SuppressLint("NewApi") public static String getPath(final Context context, final Uri uri) {

            final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

            // DocumentProvider
            if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
                // ExternalStorageProvider
                if (isExternalStorageDocument(uri)) {
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    if ("primary".equalsIgnoreCase(type)) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }

                    // handle non-primary volumes
                }
                // DownloadsProvider
                else if (isDownloadsDocument(uri)) {

                    final String id = DocumentsContract.getDocumentId(uri);
                    final Uri contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                    return getDataColumn(context, contentUri, null, null);
                }
                // MediaProvider
                else if (isMediaDocument(uri)) {
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    Uri contentUri = null;
                    if ("image".equals(type)) {
                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("video".equals(type)) {
                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(type)) {
                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }

                    final String selection = "_id=?";
                    final String[] selectionArgs = new String[] {
                            split[1]
                    };

                    return getDataColumn(context, contentUri, selection, selectionArgs);
                }
            }
            // MediaStore (and general)
            else if ("content".equalsIgnoreCase(uri.getScheme())) {

                // Return the remote address
                if (isGooglePhotosUri(uri))
                    return uri.getLastPathSegment();

                return getDataColumn(context, uri, null, null);
            }
            // File
            else if ("file".equalsIgnoreCase(uri.getScheme())) {
                return uri.getPath();
            }

            return null;
        }

它正在查找第1列。这意味着您要查找的列不在光标中。我建议您在调试器下检查光标,并查看存在哪些列。请尝试此getContentResolver().query(contentURI,null,null,“,null);第三个null参数是字符串[],而不是字符串。如果我将第二个null设置为“”,它将再次强制关闭。相同的问题或多或少存在于相同的代码中,是否有修复?谢谢,请看下面的补丁