2017-05-07 84 views
1

目前我正在尝试在图库中检索图像的位置并将其转换为文件,以便我可以将其上传到Internet。但我收到了这个错误。当我打开图库并选择它崩溃的图像时发生错误。我对Android并不熟悉,仍然认为自己是初学者。所以,如果你决定投下我的问题,请告诉我为什么我可以学习的原因。谢谢。光标错误 - 从图库中检索图像

logcat的: -

05-07 08:11:45.052 8697-8697/com.example.megasap.jobforhire_android E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.megasap.jobforhire_android, PID: 8697 
                        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:34 flg=0x1 }} to activity {com.example.megasap.jobforhire_android/com.example.megasap.jobforhire_android.ProfileEditActivity}: 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:3699) 
                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                         at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                        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.example.megasap.jobforhire_android.ProfileEditActivity.getRealPathFromURI(ProfileEditActivity.java:233) 
                         at com.example.megasap.jobforhire_android.ProfileEditActivity.onActivityResult(ProfileEditActivity.java:191) 
                         at android.app.Activity.dispatchActivityResult(Activity.java:6428) 
                         at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 
                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  
                         at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  
                         at android.os.Handler.dispatchMessage(Handler.java:102)  
                         at android.os.Looper.loop(Looper.java:148)  
                         at android.app.ActivityThread.main(ActivityThread.java:5417)  

,我用来获取图像的代码如下下来: -

private String getRealPathFromURI(Uri contentURI) { 
    String result; 
    Cursor cursor = getContentResolver().query(contentURI, null, 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); 
     result = cursor.getString(idx); // getting error here ; it returns -1 
     cursor.close(); 
    } 
    return result; 
} 
+0

你可以请张贴onActivityResult()方法? – FAT

回答

1

你必须与查询添加列名MediaStore.Images.Media.DATA

更新getRealPathFromURI()如下:

private String getRealPathFromURI(Uri contentURI) { 

    String result; 
    String[] filePathColumn = { MediaStore.Images.Media.DATA };   

    Cursor cursor = getContentResolver().query(contentURI, filePathColumn, 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(filePathColumn[0]); 
     result = cursor.getString(idx); 
     cursor.close(); 
    } 
    return result; 
} 

在你onActivityResult()方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == YOUR_REQUEST_CODE && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 
      Uri selectedImage = data.getData(); 

      // Get real path 
      String imageRealPath = getRealPathFromURI(selectedImage); 

      // Do something 

     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
       .show(); 
    } 

} 

确保您已经添加权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

这里是一个很好tutorial。希望这会有所帮助〜

+0

现在它返回NullPointerException异常而不是:( –

+0

可以请你发布的NullPointerException日志 – FAT

+0

@MohamadAzrie使用'ContentResolver的#openInputStream()'阅读你的'Uri'内容 – pskink

1

您不应该尝试获取文件路径并从该URI构建文件对象以上载文件。你不需要这一切。直接使用uri打开输入流。

某处你现在有

FileInputStream fis = new FileInputStream(path); 

替换由

InputStream is = getContentResolver().openInputStream(data.getData());