@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
- A library I want to use requires a
FILE
object to the song file and I need the full absolute path to create aFILE
object. - 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
?