3

@Anonymous's answer was really useful to get the full absolute path of a tree URI (ACTION_OPEN_DOCUMENT_TREE).

Similarly, I'd like to get the full absolute path of the URI returned from ACTION_OPEN_DOCUMENT

public void selectSong(View view) {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("audio/*");

    try {
        startActivityForResult(intent, Constants.FILE_REQUEST);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(this, "Failed to open the system file picker dialog; Are you sure you're runing Android Kitkat or up?", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == Constants.FILE_REQUEST && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            Uri uri = resultData.getData();

            if (uri != null) {
                try {
                    getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } catch (SecurityException e) {
                    e.printStackTrace();
                }

                /* I'd like to get the full absolute path of `uri` */
                //String path = getFullPathFromDocumentUri(uri);
            }
        }
    }
}

I've tried

uri.getPath(); //Returns: /document/FAEC-4302:Musics♪/Song.mp3
uri.toString(); //Returns: content://com.android.externalstorage.documents/document/FAEC-4302%3AMusics%E2%99%AA%2FSong.mp3

// `FAEC-4302` is my external SD card
// The selected file Song.mp3 is in the Musics♪ folder in the SD Card

I'd like to get an output like /storage/FAEC-4302/Musics♪/Song.mp3

The reason I'd like to get the full absolute path is that

  1. A library I want to use requires a FILE object to the song file and I need the full absolute path to create a FILE object.
  2. I want to implement a feature to save a file in the same location as the song and I need the full absolute path of the song in order to do this

How do I get the full absolute path from the URI returned by ACTION_OPEN_DOCUMENT?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • not always it has the absolute path. You likely even don't need it. How do you want to use it? – Vladyslav Matviienko May 24 '19 at 10:46
  • "A library I want to use requires a FILE object to the song file" -- you will need to make a copy of the content to a file that you control, or switch to a different library that can work with an `InputStream` or `FileDescriptor`. "I want to implement a feature to save a file in the same location as the song and I need the full absolute path of the song in order to do this" -- or, you could just persist the `Uri`, after using `takePersistableUriPermission()` to have long-term rights to access the content. – CommonsWare May 24 '19 at 11:04
  • @CommonsWare Thanks for clearing things up. About the second point though, I'm not sure how persisting the `Uri` will help. Correct me if I'm wrong, Persisting gives me long term access to the `Uri` and the content it refers to. But how does that help in saving another file in the same location as I don't have the location with me? – Spikatrix May 24 '19 at 11:26
  • "But how does that help in saving another file in the same location as I don't have the location with me?" -- it probably doesn't, and you may not have rights to work with "the same location" anyway. `ACTION_OPEN_DOCUMENT_TREE` is to get rights to work with a document tree, including creating new documents in that tree. – CommonsWare May 24 '19 at 12:12

0 Answers0