从下载文件夹中选择图像时路径错误

时间:2014-10-05 19:27:57

标签: android

我正在尝试从手机中选择一个文件并将其显示在ImageView中。当我从Gallery中选择它时,它可以正常工作但是当我从Downloads文件夹中选择它时它会崩溃。我使用的是Android 4.4.3,我知道自4.4.x版以来路径已经改变了,所以我该怎么做呢?

10-05 20:53:44.091: E/ActivityManager(949): App crashed! Process: com.example.blgui3
10-05 20:53:44.091: E/AndroidRuntime(5330): FATAL EXCEPTION: main
10-05 20:53:44.091: E/AndroidRuntime(5330): Process: com.example.blgui3, PID: 5330
10-05 20:53:44.091: E/AndroidRuntime(5330): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content: flg=0x1 }} to activity {com.example.blgui3/com.example.blgui3.NewDataActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3928)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3978)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread.access$1300(ActivityThread.java:156)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.os.Looper.loop(Looper.java:157)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread.main(ActivityThread.java:5867)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at java.lang.reflect.Method.invoke(Method.java:515)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at dalvik.system.NativeStart.main(Native Method)
10-05 20:53:44.091: E/AndroidRuntime(5330): 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.
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.database.CursorWindow.nativeGetString(Native Method)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.database.CursorWindow.getString(CursorWindow.java:492)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.database.CursorWrapper.getString(CursorWrapper.java:118)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at com.example.blgui3.NewDataActivity.getRealPathFromURI(NewDataActivity.java:606)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at com.example.blgui3.NewDataActivity.onActivityResult(NewDataActivity.java:587)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.Activity.dispatchActivityResult(Activity.java:5535)
10-05 20:53:44.091: E/AndroidRuntime(5330):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3924)

这是我的代码:

btn_gallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Complete action using"), LOAD_FROM_GALLERY);
                        dialog_newimg.dismiss();
                    }
                });

(...)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) return;

        switch (requestCode) {
            case PICK_FROM_CAMERA:              
                if (data != null) {
                    selectedImageUri = data.getData();
                    filename = getRealPathFromURI(selectedImageUri);
                    iv_photo.setImageURI(selectedImageUri); 
                    iv_photo_txt.setVisibility(View.GONE);
                    photo_added = true;
                    activity = "PICK_FROM_CAMERA";
                }                               
                break;

            case LOAD_FROM_GALLERY:     
                if (data != null) {
                    selectedImageUri = data.getData();
                    Log.i("URI", String.valueOf(selectedImageUri));
                    filename = getRealPathFromURI(selectedImageUri);
                    iv_photo.setImageURI(selectedImageUri);
                    iv_photo_txt.setVisibility(View.GONE);
                    photo_added = true;
                    activity = "LOAD_FROM_GALLERY";
                }
                break;
        }
    }

  private String getRealPathFromURI(Uri contentURI) {
        String result;
        Cursor cursor = getContentResolver().query(contentURI, 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;
    }

LOG

URI: content://com.android.providers.downloads.documents/document/2431
IDX: -1
10-05 22:40:51.446: E/CursorWindow(17553): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns

0 个答案:

没有答案