0

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.

AndreaF
  • 11,975
  • 27
  • 102
  • 168

2 Answers2

0

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So regardless the permission already requested plus the token already taken on a path. I cannot traverse its subfolders to write something and I cannot operate in any way with explicit paths. What a mess! In the best hypothesis this will kill the user experience with continuous requests for everything, and some apps certainly will be no more viable. – AndreaF Jun 25 '20 at 22:32
  • @AndreaF: If you request a tree using `ACTION_OPEN_DOCUMENT_TREE`, you can take persistable permissions and work with that tree indefinitely (barring the user deleting it or something). – CommonsWare Jun 25 '20 at 22:35
  • This will work if you have to operate only on the directory chosen without needs like generating subfolders and write and list files there. If you need to works on its subfolders SAF shows all its limits making a lot of things impossible without user intervention each time on every subfolder where you have to work (e.g. see what happens in this case https://stackoverflow.com/questions/62375696/unexpected-behavior-when-documentfile-fromtreeuri-is-called-on-uri-of-subdirec). (Unless I'm missing something) – AndreaF Jun 25 '20 at 22:45
  • No. If the user choosed a directory with ACTION_OPEN_DOCUMENT_TREE you can do what you want with that directory like creating folders, subfolders, subsubfolders and so on and files everywhere. You can do that using DocumentFile or DocumentsContract. No user intervention at all. – blackapps Jun 26 '20 at 06:30
  • @blackapps It doesn't operate without limits on subfolders already present in the directory a lot of actions are limited only to the explicitly selected folder, as you can see in my other linked question where you cannot get the folder hierarchy since the child returns always the hierarchy of father manually selected, and till now I haven't found any valid workaround for this, and DocumentsContract doesn't work either – AndreaF Jun 26 '20 at 11:07
  • Then you are doing something wrong. There is no limit. I repeat: `If the user choosed a directory with ACTION_OPEN_DOCUMENT_TREE you can do what you want with that directory like creating folders, subfolders, subsubfolders and so on and files everywhere. You can do that using DocumentFile or DocumentsContract. No user intervention at all.`. That includes for all subdirectories and files already present. – blackapps Jun 26 '20 at 18:40
-1

If the user once choosed /storage/emulated/0 with ACTION_OPEN_DOCUMENT_TREE then you can construct the uri for a path like /storage/emulated/0/myfolder/mysubfolder/myfile quite easily yourself. And use the uri for full access.

It is a little bit the reverse of functions like getRealPathFromUri().

Bear in mind that those functions are cursed!

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • If I use `ACTION_OPEN_DOCUMENT_TREE ` most of the point to construct Uri is invalidated since I could use it directly, but I want to operate in the path programmately in order to create a folder structure according some directives generated without forcing user to do a lot of actions manually. – AndreaF Jun 26 '20 at 11:35