9
votes

for example

real path is mnt/sdcard/image_1.jpg Uri path is content://media/external/images/media/140 like this

  Uri photoUri =Uri.parse("content://media/external/images/media/140");
                                            Log.d("selectedphoto",""+photoUri);
               selectedImagePath = getPath(photoUri);

     public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();



    return cursor.getString(column_index);
    }

In above code I convert Uri to real path but dnt know how to convert real path to Uri

2

2 Answers

14
votes

Try this:

Uri.fromFile(new File("/sdcard/cats.jpg"));
0
votes

This will get the file path from the MediaProvider, DownloadsProvider, and ExternalStorageProvider, while falling back to the unofficial ContentProvider method you mention.

https://stackoverflow.com/a/27271131/3758898