I am wanting to use Android Scoped Storage for accessing both the local files directory and the SD card. My problem is with using the same functions to access both, for which I need the Uri for the local files directory.
I would have thought that the first two lines of code below would give me the Uri of the local files directory, but the third line, which is how I would use the Uri, raises an exception with this detail message:
Invalid URI: file:///data/user/0/tl.listmster2/files
val file = DocumentFile.fromFile(context.filesDir) val uri = file.uri val dir = DocumentFile.fromTreeUri(context, uri)
I'm sure that there a workarounds, but it would be really helpful to get a valid Uri of the local files directory, if anyone has any ideas? Thanks in advance.
My interpretation is that by "the same functions to access both", you were planning on using
fromTreeUri()for both documents and files. That will not work.fromTreeUri()only works with a document treeUri.My guess is that those "same functions" should be taking a
DocumentFileas input, not aUri. Code outside of those functions can know where theDocumentFilecame from (fromFile(),fromTreeUri(), orfromSingleUri()). Those functions can deal with theDocumentFileabstraction, using methods likeisDirectory()andlistFiles()to traverse the contents as needed.There is no
Uribased off aFilethat works withfromTreeUri().