-1

I want to get the absolute path of a file which is selected from a file chooser.
My OnActivityesult() is like below:

 if(resultCode == RESULT_OK) {
        switch(requestCode) {
            case 1:
                Uri pathUri = data.getData();
                File userFile = new File(pathUri.getPath());
                IMG_PATH = userFile.getParentFile().getAbsolutePath();
                Snackbar.make(getCurrentFocus(), IMG_PATH, Snackbar.LENGTH_SHORT).show();
                ShowConfirmation(IMG_PATH);
                break;
        }
    }

but this code returns like /document/primary:miniclipId.txt.
I need the real path like mnt/sdcard1/miniclipId.txt.
The user will choose .img file from sdcard or internal.
Can anybody tell me how can i get the path string like that?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kannan
  • 51
  • 1
  • 8
  • Possible duplicate of [Get filepath from google drive in Lollipop (MediaStore.MediaColumns.DATA == null)](http://stackoverflow.com/questions/29378651/get-filepath-from-google-drive-in-lollipop-mediastore-mediacolumns-data-null) – user1643723 Apr 02 '16 at 11:14
  • This code is for choosing `.img` file from sdcard, not media files – Kannan Apr 02 '16 at 11:21
  • The source of file is irrelevant. I recommend you to read the answer to linked question. – user1643723 Apr 02 '16 at 11:23

3 Answers3

4

but this code returen like /document/primary:miniclipId.txt.

You received a Uri from a ContentProvider.

I need the real path like mnt/sdcard1/miniclipId.txt.

A Uri is not a file. There is no requirement that there be a "real path", let alone one that is meaningful to you.

Use ContentResolver and methods like openInputStream() to consume the content pointed to by the Uri.

If you absolutely need a file, use openInputStream() and copy the content to some file that you control.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • If there are relative paths in the file to other files and I need to use them within cross-compiled libraries. Is there a clever way to do that? – Matthias Kuhn Aug 27 '18 at 11:59
  • @MatthiasKuhn: If by "in the file", you mean "in the content identified by the `Uri`", then there is no reliable way to handle what you are seeking. – CommonsWare Aug 27 '18 at 12:23
  • Is there a (reliable?) way to handle the subset of cases where the content identified by the `Uri` is a local file? – Matthias Kuhn Aug 28 '18 at 03:59
  • 1
    @MatthiasKuhn: If you only want to work with local files, stop using things involving `Uri` values. Instead, [use a file chooser library](https://android-arsenal.com/tag/35). This will limit you to files that you can access via the filesystem, and then normal filesystem traversal rules apply for things like relative paths. – CommonsWare Aug 28 '18 at 10:59
  • Thanks a lot, that sounds like the answer I was looking for! – Matthias Kuhn Aug 28 '18 at 12:05
0

u can use this function to get real path of media.u just have to pass uri that u get in onActivityResult() method

 public String mf_szGetRealPathFromURI(final Uri ac_Uri) {
    String result = "";
    boolean isok = false;
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = getContentResolver().query(ac_Uri, proj,
                null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
        isok = true;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return isok ? result : "";
}
Sagar
  • 585
  • 1
  • 9
  • 28
0

This is what I'm using for years

public static String getRealPathFromURI(Uri contentUri, Context activity) {
    String path = null;
    try {
        final String[] proj = {MediaStore.MediaColumns.DATA};
        final Cursor cursor = ((Activity) activity).managedQuery(contentUri, proj, null, null, null);
        final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index);
    } catch (Exception e) {
    }
    if (path != null && path.length() > 0) {
        return path;
    } else return contentUri.getPath();
}
pavko_a
  • 507
  • 4
  • 16