[英]ACTION_GET_CONTENT only files, no images or videos
Ok i start my func with this好的,我用这个开始我的功能
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
for Samsung三星
chooserIntent = Intent.createChooser(sIntent, context.getString(R.string.choosefilefrom));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intent});
for other devices对于其他设备
chooserIntent = Intent.createChooser(intent, context.getString(R.string.choosefilefrom));
mimeType are the types are allowed to use, its at this moment */*
. mimeType 是允许使用的类型,此时*/*
。 but how to exclude the images and videos to show only files in the file picker?但是如何排除图像和视频以仅显示文件选择器中的文件? i have 3 intents, one for images, one for videos and one for files.我有 3 种意图,一种用于图像,一种用于视频,一种用于文件。
If you take a look at https://www.iana.org/assignments/media-types/media-types.xhtml如果你看看https://www.iana.org/assignments/media-types/media-types.xhtml
Then you can probably assume the main mime types:那么你大概可以假设主要的 mime 类型:
application/ogg
) application/* (很多文件类型,不包括以下主要类型,但包括音频,如application/ogg
) So assuming you want to be able to select anything else other then Audio
or Video
or perhaps Images
因此,假设您希望能够选择除Audio
或Video
或Images
任何其他内容
Then the following should do the trick:那么以下应该可以解决问题:
String[] mimetypes = {
"application/*",
//"audio/*",
"font/*",
//"image/*",
"message/*",
"model/*",
"multipart/*",
"text/*",
//"video/*"
};
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); //Important part here
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 10000);
Then of course to handle the returned URIs:然后当然要处理返回的 URI:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10000)
{
if (data != null)
{
if (data.getClipData() != null)
{
// multiple files selected
for (int i = 0; i < data.getClipData().getItemCount(); i++)
{
android.net.Uri uri = data.getClipData().getItemAt(i).getUri();
}
}
else
{
// single file selected
android.net.Uri uri = data.getData();
}
}
}
}
Here is how to properly get all the Uri's
:以下是正确获取所有Uri's
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10000)
{
if (data != null)
{
List<Uri> sharedUris = getUrisFromIntent(data);
}
}
}
Then the getUrisFromIntent
method:然后是getUrisFromIntent
方法:
public static List<Uri> getUrisFromIntent(Intent intent) {
List<Uri> uriList = new ArrayList<>();
if (intent.getData() != null) {
uriList.add(intent.getData());
} else if (intent.getClipData() != null) {
for (int i = 0; i < intent.getClipData().getItemCount(); i++) {
uriList.add(intent.getClipData().getItemAt(i).getUri());
}
} else {
if (intent.getAction().equals(Intent.ACTION_SEND)) {
if (intent.hasExtra(Intent.EXTRA_STREAM)) {
Uri sharedFile = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (sharedFile != null) {
uriList.add(sharedFile);
}
}
} else if (intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
if (intent.hasExtra(Intent.EXTRA_STREAM)) {
List<Uri> sharedFiles = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (sharedFiles != null) {
uriList.addAll(sharedFiles);
}
}
}
}
return uriList;
}
This is tested and works on Android API 17 - 29这在 Android API 17 - 29上经过测试和工作
You are currently specifying / , which is "everything".您当前正在指定/ ,即“一切”。
As far as I know, there is no mechanism for exclusion in mime types.据我所知,在 mime 类型中没有排除机制。
You would have to specify specific mime types that you want to allow - there's no way to specify "everything but these two mime types".您必须指定要允许的特定 mime 类型 - 无法指定“除了这两种 mime 类型之外的所有内容”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.