1

After choosing an image from gallery i want to upload it with retrofit2.

https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server

to do this i have to get the real file path.

allImgUris.get(0) gives me:

content://com.android.providers.downloads.documents/document/5

allImgUris.get(0).getPath() gives me:

/document/5

i need to pass the real file path to:

 File file = new File(allImgUris.get(0));

i found a lot of supposedly 'good' answers concerning functions dealing with context and cursors but none of them work.

any idea how to solve this?

seems like something very trivial but turns out to be eating hours of research time.

any help would be greatly appreciated!

not working:

            Cursor cursor = getApplicationContext().getContentResolver().query(allImgUris.get(0), null, null, null, null);
            if (cursor == null) {
                 selectedImagePath = allImgUris.get(0).getPath();
            } else {
                cursor.moveToFirst();
                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                selectedImagePath = cursor.getString(idx);  // ava.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                cursor.close();
            }

gives:

Java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

Also not working:

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

String selectedImagePath = getRealPathFromURI(getApplicationContext(),allImgUris.get(0) );

returns null

dilux
  • 277
  • 1
  • 3
  • 10

1 Answers1

0

apparently the way to go is get the bitmap and save it to filesystem again and retrieve path that way.

Saving and Reading Bitmaps/Images from Internal memory in Android

Community
  • 1
  • 1
dilux
  • 277
  • 1
  • 3
  • 10