Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 989
Hong Avatar asked Sep 19 '16 16:09

Hong


People also ask

What is Documentfile?

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.

What is storage access framework?

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.


2 Answers

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.

like image 137
CommonsWare Avatar answered Oct 01 '22 03:10

CommonsWare


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 ")"

like image 28
JMP M3dia Avatar answered Oct 01 '22 02:10

JMP M3dia