For Android, the following code returns a Uri that can be used to create DocumentFile corresponding to a directory.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_CUSTOM_FOLDER);
Since many methods of a library require a parameter to be java.io.File, I am wondering if one can get a java.io.File from a DocumentFile.
As an example, an returned document tree Uri treeUri is the following:
treeUri.getPath():/tree/primary:DCIM/deeper/evendeeper
treeUri.toString(): content://com.android.externalstorage.documents/tree/primary%3ADCIM%2Fdeeper%2Fevendeeper
The following is the directory shown in ADM:
Representation of a document backed by either a android. provider. DocumentsProvider or a raw file on disk. This is a utility class designed to emulate the traditional File interface. It offers a simplified view of a tree of documents, but it has substantial overhead.
The SAF makes it simple for users to browse and open documents, images, and other files across all of their preferred document storage providers. A standard, easy-to-use UI lets users browse files and access recents in a consistent way across apps and providers.
the following code returns a Uri that can be used to create DocumentFile corresponding to a directory
No, it does not. It returns a Uri
that can be used to create a DocumentFile
corresponding to a tree of documents. There is no requirement for it to represent a directory on a filesystem. Where and how the document tree is represented is up to the DocumentsProvider
that the user chose. That provider can do whatever it wants to represent the tree (e.g., use a database, use REST calls to a server).
I am wondering if one can get a java.io.File from a DocumentFile.
No, because there is no file.
Yes, but it may be that the address is in ASCII and you must convert it to UTF-8 to avoid text strings like "%2F". I recommend using "val decoded = URLDecoder.decode (input," utf-8 ")"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With