0

I am selecting a picture from the android device library as follows

public static void showFileChooser(Activity activity) {
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}

and trying to get realPathFromURI as follows, but idx always returns -1. I wonder what I am missing.

enter image description here

Manifest file

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

Update 1:

enter image description here

Update 2: I even tried the following, path returns null

enter image description here

casillas
  • 16,351
  • 19
  • 115
  • 215

2 Answers2

1

If you are selecting image from devices synced folder then it must be the problem with image URI, Checkout this question and this answer.

Hope this will help you !!!

Community
  • 1
  • 1
Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
0

use media projection

 public static String getPath(Uri uri, Activity mActivity) {        
            try {
                String[] projection = {MediaStore.MediaColumns.DATA};
                Cursor cursor = mActivity.managedQuery(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();           
                return cursor.getString(column_index);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                return "";
            } 
        }

Just use getContentResolver().openInputStream(uri) to get an InputStream from a URI. in case of drive path

if input stream contains null you need to integrate drive api

AMD
  • 1,662
  • 18
  • 39