从Uri获取文件的realPath时光标出现错误

时间:2019-01-24 14:51:26

标签: android cursor

我正在从我的应用访问外部存储中的文件。从URI查找文件的真实路径时,它始终可以正常工作。但是只有在极少数情况下,此代码才会在游标中引发错误。

 public static String getRealPathFromURI(Activity activity, Uri contentURI) {
        String result;
        Cursor cursor = null;
        cursor = activity.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);
            cursor.close();
        }
        return result;
    }

我的错误日志

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/520 flg=0x43 }} to activity {com.tachyloans/com.tachyloans.activity.PanActivity}: 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:4359)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4403)
        at android.app.ActivityThread.-wrap19(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1694)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     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:137)
        at com.tachyloans.common.Utils.getRealPathFromURI(Utils.java:414)
        at com.tachyloans.activity.PanActivity.onActivityResult(PanActivity.java:356)
        at android.app.Activity.dispatchActivityResult(Activity.java:7341)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4355)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4403) 
        at android.app.ActivityThread.-wrap19(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1694) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

0 个答案:

没有答案