androidandroid-filestorage-access-frameworkdocumentfile

Is it possible to get a regular File from DocumentFile?


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: enter image description here


Solution

  • 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.