The method Uri.parse
generates an Uri that is not an Android tree Uri if I try to parse a String path of the kind "String path=/storage/emulated/0/Somefolder/"
.
For this reason if I try to get DocumemtFile
list with something like
String path="/storage/emulated/0/Somefolder/"
DocumentFiles[] fileslist=DocumentFile.fromTreeUri(this, Uri.parse(path)).listFiles();
I get Invalid URI error.
And if instead I try to use DocumentFile.fromSingleUri
doesn't work correctly too.
How Could I get the tree Uri correctly from a String path?
I have found how to get the treeuri using action Intent , but I need to do this programmatically without the needs of forcing the user to choose the folder manually.
How Could I get the tree Uri correctly from a String path?
That is not possible, sorry.
For the purposes of having a DocumentFile
pointing to a filesystem path, use fromFile()
. Just bear in mind that:
You need android:requestLegacyExternalStorage="true"
on your <application>
element in the manifest to have read access to arbitrary filesystem paths on Android 10+
You will lose write access to arbitrary filesystem paths (despite that attribute) on Android 11+ once your targetSdkVersion
hits 30, sometime next year