开发者

MIME type for directories in Android

开发者 https://www.devze.com 2023-02-05 13:42 出处:网络
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:

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


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


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.


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消