Suppose a Uri can be either of the following:
- A Uri from a Storage Access Framework's DocumentFile (i.e. DocumentFile.getUri()).
- A Uri from a regular File (i.e. Uri.fromFile(File))
It refers to a file under a directory in both situations.
Is there a straightforward way to get its parent directory's Uri without trying each of the two to see which one works?
[Edit]: Here is an example for SAF:
Uri:
content://com.android.externalstorage.documents/tree/0000-0000%3Atest/document/0000-0000%3Atest%2Ffoo%2FMovies%2FRR%20parking%20lot%20a%202018_02_22_075101.mp4
getPath():
/tree/0000-0000:test/document/0000-0000:test/foo/Movies/RR parking lot a 2018_02_22_075101.mp4
getPathSegments():
0 = "tree"
1 = "0000-0000:test"
2 = "document"
3 = "0000-0000:test/foo/Movies/RR parking lot a 2018_02_22_075101.mp4"
The parent folder should be test/foo/Movies.
Here is an example for a regular file:
Uri:
file:///storage/emulated/0/foo/Movies/RR%20parking%20lot%20a%202018_02_22_081351.mp4
getPath():
/storage/emulated/0/foo/Movies/RR parking lot a 2018_02_22_081351.mp4
getPathSegments():
0 = "storage"
1 = "emulated"
2 = "0"
3 = "foo"
4 = "Movies"
5 = "RR parking lot a 2018_02_22_081351.mp4"