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

Man*_*ddy 5 android android-intent

我正在使用Intent打开文件管理器,我需要知道如何只显示.doc,.docx文件供用户选择.如何将setType放入intent?`Follow函数用于从文件管理器中选择文件.

    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);
}`
Run Code Online (Sandbox Code Playgroud)

Vla*_*nko 10

您可以添加多个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);
Run Code Online (Sandbox Code Playgroud)

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

String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
Run Code Online (Sandbox Code Playgroud)

  • @AlitonOliveira,这很明显,至少因为问题是关于`doc`、`docx` 格式的。 (2认同)