通过Android中的意图限制可选文件类型 [英] Restricting selectable file types via intent in Android

查看:539
本文介绍了通过Android中的意图限制可选文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将所选文件上传到服务器,但是我想限制用户仅选择文档文件(.doc,.pdf等)和图像文件.

I am uploading selected files to server but I want to restrict users to pick only document files (.doc, .pdf, etc.) and image files.

目前,我的代码适用于所有文件,它可以提取所有文件的uri.

For now my code is working for all files it fetches uri of all files.

如何限制用户仅选择特定类型的文件?

How can I restrict users to pick only specific types of file?

这是我选择任何文件的代码.

Here is my code to pick any file.

Intent i=new Intent();
i.setType("*/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(i, "abc"),requestCode);

推荐答案

通过|分别传递多个MIME类型 喜欢

Pass multiple MIME types separate with | like

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

或创建一个MIME类型数组,例如

or create an array of MIME types like

String[] mimetypes = {"image/*", "application/*|text/*"};

并将其传递为

i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);

这篇关于通过Android中的意图限制可选文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆