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

时间:2014-08-04 16:59:11

标签: 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 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卡的根目录添加图像,因此它必须是下载文件夹...

1 个答案:

答案 0 :(得分:-1)

我设法做到了,因为事实证明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;
        }