3

Currently I'm trying to retrieve the location of an image in my gallery and convert it into a File so that I can upload it to the internet. But I received this error instead. The error happened when I open the gallery and on selecting the image it crash instead. I'm not so well versed in android and still consider myself a beginner. So if you decided to vote down my question please tell me the reason why so that I can learn. Thank you.

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) 

The code that I used to retrieved the image is down below:-

 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;
}
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
azriebakri
  • 1,131
  • 2
  • 11
  • 36

3 Answers3

2

You have to add column name MediaStore.Images.Media.DATA with query.

Update getRealPathFromURI() as below:

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;
}

In you onActivityResult() method:

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();
    }

}

Make sure you have added permission:

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

Here is a good tutorial. Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
1

You should not try to get a file path and construct a file object from that uri to upload the file. You dont need that all. Use the uri directly to open an inputstream.

Somewhere you will now have

FileInputStream fis = new FileInputStream(path);

Replace that by

InputStream is = getContentResolver().openInputStream(data.getData());
greenapps
  • 11,154
  • 2
  • 16
  • 19
1

This supports APIs below and above 19, including 19:

 public String getRealPathFromURI(Uri uri) {

        Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
        String result;

        // for API 19 and above
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {


            cursor.moveToFirst();
            String image_id = cursor.getString(0);
            image_id = image_id.substring(image_id.lastIndexOf(":") + 1);
            cursor.close();

            cursor = this.getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);

        }

        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        cursor.close();
        return result;
    }
Marija
  • 422
  • 4
  • 12