Android 中的多种 MIME 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:28:48  来源:igfitidea点击:

Multiple MIME types in Android

androidandroid-intentmime-types

提问by James

Is there a way to use intent.setType()and supply multiple broad types (like images andvideo)?

有没有办法使用intent.setType()和提供多种广泛类型(如图像视频)?

I am using an ACTION_GET_CONTENT. It seems to be working with just comma-separated types.

我正在使用一个ACTION_GET_CONTENT. 它似乎只使用逗号分隔的类型。

回答by Fred

In Android 4.4 when using the Storage Access Frameworkyou can use the EXTRA_MIME_TYPESto pass multiple mime types.

在 Android 4.4 中,当使用Storage Access Framework 时,您可以使用EXTRA_MIME_TYPES来传递多种 mime 类型。

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE_OPEN);

回答by bazyle

Actually, multiple mime-types are supported. Have you even tried it???

实际上,支持多种 MIME 类型。你有没有试过???

For example: intent.setType("image/*,video/*")will display photos and videos...

例如:intent.setType("image/*,video/*")将显示照片和视频...

For me it works. It should work for you too...

对我来说它有效。它也应该对你有用......

[EDIT]: This works partially, as not all the gallery apps choose to implement support for multiple mime types filters.

[编辑]:这部分有效,因为并非所有图库应用程序都选择实现对多种 mime 类型过滤器的支持。

回答by Raimundo

For me what worked best was:

对我来说,效果最好的是:

intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);



You can add several mime types like this

您可以像这样添加多种 mime 类型

intent.setType("image/*|application/pdf|audio/*");

But the intent chooser will only display applications that can handle images because it is the first in the mime type string.

However if you have a file manager installed (I tested with the CyanogenMod file manager) you can choose a file that is audio or pdf or an image.

If the audio mime type is the first one, like this:

但是意图选择器只会显示可以处理图像的应用程序,因为它是 mime 类型字符串中的第一个。

但是,如果您安装了文件管理器(我使用 CyanogenMod 文件管理器进行了测试),您可以选择音频、pdf 或图像文件。

如果音频 mime 类型是第一个,如下所示:

intent.setType("audio/*|image/*|application/pdf");

The intent chooser will display only applications that handle audio.
Again using the file manager you can select an image or pdf or audio.

意图选择器将仅显示处理音频的应用程序。
再次使用文件管理器,您可以选择图像、pdf 或音频。

回答by hackbod

Sorry, this is not currently supported. You have two options:

抱歉,目前不支持此功能。您有两个选择:

(1) Use a MIME type of */*and accept that there may be some things the user can pick that you won't be able to handle (and have a decent recovery path for that); or

(1) 使用 MIME 类型*/*并接受用户可能会选择您无法处理的某些事情(并为此提供合适的恢复路径);或者

(2) Implement your own activity chooser, doing direct calls on the package manager to get the activities that can handle both MIME types for the intent, merging those lists, and displaying them to the user.

(2) 实现您自己的活动选择器,直接调用包管理器以获取可以处理意图的两种 MIME 类型的活动,合并这些列表,并将它们显示给用户。

Also, setType()does not work with comma-separated types at all. It must be one and only one MIME type.

此外,setType()根本不适用于逗号分隔的类型。它必须是一种且只有一种 MIME 类型。

回答by Cifus

you can pass multiple mime types if you separate with |

如果你用 | 分开,你可以传递多种 mime 类型

Intent.setType("application/*|text/*");

回答by javier

for my work with semicolons.

对于我使用分号的工作。

Example:

例子:

intent.setType("image/*;video/*")

or

或者

sIntent.putExtra("CONTENT_TYPE", "image/*;video/*");