如何通过意图仅选择doc,docx文件?

时间:2016-11-29 12:09:32

标签: android android-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 个答案:

答案 0 :(得分:9)

您可以添加多个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"};