如何通过意图(Intent)仅选择doc、docx文件?

13

我正在使用Intent打开文件管理器,我需要知道如何仅显示.doc和.docx文件供用户选择。 如何将setType放入Intent中?以下功能用于从文件管理器选择文件。

我正在使用Intent打开文件管理器,我需要知道如何仅显示.doc和.docx文件供用户选择。如何将setType设置给intent?以下函数用于从文件管理器选择文件。

    private void showFileChooser() {
    Intent intent = new Intent();
    //sets the select file to all types of files
    intent.setType("application/*");

    //allows to select data and return it
    intent.setAction(Intent.ACTION_GET_CONTENT);
    //starts new activity to select file and return data
    startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}`

1
这是你所寻找的吗?https://dev59.com/YGox5IYBdhLWcg3w1Xo3#17949893 - zombie
有iOS的任何想法吗? - Saket Kumar
1个回答

21
您可以像这样添加多个MIME类型:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE_OPEN);

以下 MIME 类型对应于 .docx.doc 文件。

String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};

1
这组命令不适用于 Excel 文件。会出现一条消息:“没有应用程序可以执行此操作”。我使用了以下命令行处理 Excel 文件:String[] mimetypes = {"application/vnd.ms-excel" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}; - Aliton Oliveira
2
@AlitonOliveira,这显然是因为问题涉及到“doc”和“docx”格式。 - Vladyslav Matviienko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接