Hong Hong - 2 months ago 15
Android Question

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 requires a parameter to be java.io.File, I am wondering if one can get a java.io.File from a DocumentFile.

Answer

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.