-->

MIME type for directories in Android

2020-08-17 06:47发布

问题:

I was wondering if I can start an Intent for viewing a directory with a File browser (if there's one installed on the device) so I can open a folder like this:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/MyFolder");
intent.setDataAndType(uri, "MIME TYPE FOR FOLDERS");
startActivity(intent);

回答1:

AndExplorer has vendor mime types to use AndExplorer as a file chooser:

  • vnd.android.cursor.dir/lysesoft.andexplorer.director
  • vnd.android.cursor.dir/lysesoft.andexplorer.file

See AndExplorer's developper documentation for more information. I think other file explorers as similar features, but I didn't find their docs yet.



回答2:

This is an ancient question but I noticed that the directory MimeType had not actually been specified properly in any of the answers. I found this in the Google code sample for SAF (Storage Access Framework) android-StorageProvider:

DocumentsContract.Document.MIME_TYPE_DIR

Which is this string:

vnd.android.document/directory

So for the initial question:

    Uri uri = Uri.parse(getFilesDir().getPath());
    int OPEN_REQUEST = 1337;
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setDataAndType(uri, DocumentsContract.Document.MIME_TYPE_DIR);

    startActivityForResult(intent, OPEN_REQUEST);

But the Storage Access Framework will navigate through the directories for you. So you may only need to do this:

    int OPEN_REQUEST = 1337;
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");

    startActivityForResult(intent, OPEN_REQUEST);


回答3:

The strings 'inode/directory' and 'x-directory/normal' seem to be pre-existing MIME types for Unix-based systems, according to a few Google searches, but nothing I have on my phone knows about either so I'll probably check for and offer those, before looking for vnd.android.cursor.item/directory, which at least has the value of not binding to a particular application.

I'd be disinclined to use an application-specific type.

(I just found that ES uses 'resource/folder' which is also plausibly application-independent; too bad there's no way to tell Android that these types are all equivalent.)



回答4:

Blackmoon File Browser responds to the MIME type of

  • vnd.android.cursor.item/file

in order to open it's view so that the contents of a folder are shown or, in the case of a file, it's parent folder is shown scrolled to show that particular file.

You can view Blackmoon's dev documentation outlining various intents it uses as well.

It's too bad that Android hasn't specified a particular MIME type for viewing a file/folder in a file browsing app. This way all file management apps would use the same intents so that users can freely choose between whichever apps works best for them rather than being locked into any particular app.

I guess that's why such site as http://www.openintents.org/ exist so that developers can share some standards or at least publish some standard app capabilities.



回答5:

According to the official docs it is vnd.android.cursor.dir/*



回答6:

Because files that are directories don't seem to have a standard mime type, I coded my file explorer to filter for intents without a mime type. Just set the data to your folder's Uri. This also sets the intent scheme to "file", which is required by my intent filter.

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/MyFolder");
intent.setData(uri);
startActivity(intent);

This intent will launch following app:

http://play.google.com/store/apps/details?id=com.organicsystemsllc.thumbdrive