Question: I'm trying to get image from gallery. 

```
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select picture"), resultCode );

```

After I returned from this activity I have a data, which contains Uri. It looks like:

```
content://media/external/images/1

```

How can I convert this path to real one (just like '`/sdcard/image.png`') ?

Thanks

Answer: 
**Note** This is an improvement in [@user3516549 answer](https://stackoverflow.com/questions/2789276/android-get-real-path-by-uri-getpath/33074944#33074944) and I have check it on Moto G3 with Android 6.0.1  

I have this issue so I have tried answer of @user3516549 but in some cases it was not working properly.
I have found that in Android 6.0(or above) when we start gallery image pick intent then a screen will open that shows recent images when user select image from this list we will get uri as

```
content://com.android.providers.media.documents/document/image%3A52530

```

while if user select gallery from sliding drawer instead of recent then we will get uri as

```
content://media/external/images/media/52530

```

So I have handle it in **`getRealPathFromURI_API19()`**

```
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    if (uri.getHost().contains("com.android.providers.media")) {
        // Image pick from recent 
        String wholeID = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

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

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    } else {
        // image pick from gallery 
       return  getRealPathFromURI_BelowAPI11(context,uri)
    }
}

```

**EDIT1 :** if you are trying to get image path of file in external sdcard in higher version then check [my question](https://stackoverflow.com/questions/42110882/)

**EDIT2** [Here](https://gist.github.com/jaisoni/4d4e20409849dd3cfa12ee15895124a6) is complete code with handling virtual files and host other than `com.android.providers` I have tested this method with `content://com.adobe.scan.android.documents/document/`
This code work for me in android 11 and 12

```
private static String getRealPathFromURI(Uri uri, Context context) {
                Uri returnUri = uri;
                Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
                int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                returnCursor.moveToFirst();
                String name = (returnCursor.getString(nameIndex));
                String size = (Long.toString(returnCursor.getLong(sizeIndex)));
                File file = new File(context.getFilesDir(), name);
                try {
                    InputStream inputStream = context.getContentResolver().openInputStream(uri);
                    FileOutputStream outputStream = new FileOutputStream(file);
                    int read = 0;
                    int maxBufferSize = 1 * 1024 * 1024;
                    int bytesAvailable = inputStream.available();

                    //int bufferSize = 1024;
                    int bufferSize = Math.min(bytesAvailable, maxBufferSize);

                    final byte[] buffers = new byte[bufferSize];
                    while ((read = inputStream.read(buffers)) != -1) {
                        outputStream.write(buffers, 0, read);
                    }
                    Log.e("File Size", "Size " + file.length());
                    inputStream.close();
                    outputStream.close();
                    Log.e("File Path", "Path " + file.getPath());
                    Log.e("File Size", "Size " + file.length());
                } catch (Exception e) {
                    Log.e("Exception", e.getMessage());
                }
                return file.getPath();
            }

```