【问题标题】:How to choose only doc,docx files through intent?如何通过意图仅选择doc,docx文件?
【发布时间】:2017-04-13 10:20:56
【问题描述】:

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

    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);
}`

【问题讨论】:

标签: android android-intent


【解决方案1】:

您可以像这样添加多种 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"};

【讨论】:

  • 这组命令不适用于 excel 文件。显示一条消息:没有应用程序可以执行此操作。我将这行命令用于 excel 文件:String[] mimetypes = {"application/vnd.ms-excel" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};
  • @AlitonOliveira,这很明显,至少因为问题是关于 docdocx 格式的。
猜你喜欢
  • 2022-08-17
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
相关资源
最近更新 更多