【问题标题】:Select a file on Android with given extension / MIME time在 Android 上选择具有给定扩展名/MIME 时间的文件
【发布时间】:2014-12-10 15:40:31
【问题描述】:

我正在使用以下代码从任何可用来源(本地存储、谷歌驱动器、投递箱等)打开一个 .zip 文件:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
String mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension("zip");

intent.setType(mimetype);

Intent chooser = Intent.createChooser(intent, "Select a zip file");

if (intent.resolveActivity(getPackageManager())!=null)
    startActivityForResult(chooser, 1);

这在我的 Nexus 5(Android 5.0)和我的 Nexus 7(Andorid 4.4.4)上运行良好。标准的 Android 对话框显示允许我从谷歌驱动器、下拉框等中选择 zip 文件。与请求的扩展名不匹配的文件是不可选择的。完美!

但是当我使用 Andoroid 4.4.2 在 Samsung Tab 上对其进行测试时,它只显示 Dropbox 文件选择器对话框...没有谷歌驱动器,没有本地存储,没有文件管理器应用程序... 请考虑上面代码中的mimetype 等于application/zip。如果我将代码更改为intent.setType("*/*");,它现在会显示 Nexus 5 中的所有选项(谷歌驱动器、保管箱、本地存储等),但是我可以选择任何文件,而不仅仅是 zip,这不是我想要的。

【问题讨论】:

  • 我们在为客户开发的应用程序中遇到了与您完全相同的问题。但我们尝试打开 PDF 文件而不是 ZIP。您是否已经找到了解决方案?
  • ...没有最后我放弃了...我使用intent.setType("*/*");然后我检查用户选择的文件是否实际上是一个 zip 文件(在你的情况下是 pdf 文件)。真烦人……

标签: android mime-types


【解决方案1】:

你试过了吗?

intent.setType("application/zip|application/x-rar-compressed");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多