1

I'm taking picture from camera and saving in a public folder(Pictures/myFolder) and I'm storing the Uri from picture to reload to my views, but I need build a File with real path, but I cant recover the path and all the codes they find on the internet give me null pointer, how can i recover the real path?

Uri example:

content://media/content://br.com.technolog.darwinchecklist.fileprovider/darwin_checklist_images/DARWIN_20180827_114154_460340375.jpg/images/media

Method that does not work

public String getRealPathFromURI(Uri uri) {
String path = "";
if (getContentResolver() != null) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path = cursor.getString(idx);
        cursor.close();
    }
}
return path;

}

Bracadabra
  • 3,609
  • 3
  • 26
  • 46
Felipe Custódio
  • 102
  • 4
  • 13
  • `content://media/content://br.com.technolog.darwinchecklist.fileprovider/darwin_checklist_images/DARWIN_20180827_114154_460340375.jpg/images/media` doesn't seem to be right. You're expecting to find the `/images/media` folder in a `DARWIN_20180827_114154_460340375.jpg` file. – Shark Aug 31 '18 at 14:28

2 Answers2

0

Ref: Get Real Path For Uri Android

getRealpathFromUri(Uri uri)
{
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
if (cursor == null) 
{ // Source is Dropbox or other similar local file path
    result = contentURI.getPath();
}
else 
{ 
        if(cursor.moveToFirst()){
   int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
   //String yourRealPath = cursor.getString(columnIndex);
   path = cursor.getString(columnIndex);
         }
    cursor.close();
}
return path;
}
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
0

I'm using the following code to get the real path of a document from an Uri:

/**
 * Retrieve filename from Uri
 *
 * @param uri             Uri following the schemes: "file" or "content"
 * @param contentResolver ContentResolver to resolve content scheme
 *
 * @return filename if operation succeded. Can be null.
 */
public static String getFileName(@NonNull final Uri uri, @NonNull final ContentResolver contentResolver) {
    String filename = "";
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        filename = uri.getLastPathSegment();
    } else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        try {
            Cursor cursor = contentResolver.query(uri, null, null, null, null);
            int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            filename = cursor.getString(index);
            cursor.close();
        } catch (Exception e) {
            Log.e(TAG, "Exception when retrieving file name: " + e.getMessage());
        }
    }

    return filename;
}
Bruno
  • 3,872
  • 4
  • 20
  • 37