Search code examples
androidfile-descriptorandroid-fileandroid-contentresolverandroid-fileprovider

Get files from directory's FileDescriptor


I have content uri of directory from other app's share intent. From this I can create FileDescriptor and open InputStream. But how can I check if it's directory and get files if it's directory? Thanks!


Solution

  • I have content uri of directory from other app's share intent

    There is no such thing as "content uri of directory", any more than there is a "REST URL of directory".

    In certain cases, we may have a protocol where a Uri represents a collection of content (e.g., ContactsContract.Contacts.CONTENT_URI, a Uri retrieved via ACTION_OPEN_DOCUMENT_TREE). There is no such protocol for "content uri of directory", delivered via ACTION_SEND, in standard Android.

    From this I can create FileDescriptor and open InputStream

    Possibly. That depends entirely upon what the Uri actually represents.

    But how can I check if it's directory and get files if it's directory?

    In general, you can't.

    If this Uri happens to be backed by a document provider, and the Uri happens to represent a tree of content, DocumentFile.fromTreeUri() will let you list the documents in that tree, akin to the API on File.

    Otherwise, you are welcome to look at the MIME type of the Uri. If the MIME type starts with vnd.android.cursor.dir/, then it should be a Uri that you can query(), though there are no rules for what columns there are. If the MIME type is anything else, there are no rules for what the content is and how you can use it.

    For specific apps, you can contact the developers of the app and ask them "hey, when you give me a Uri that looks like this, how can I use it?".

    TL;DR: if you are given a Uri via ACTION_SEND, where that Uri points to anything that is not a piece of content, the sending app has a bug.