【问题标题】:Launching an intent for file and MIME type?启动文件和 MIME 类型的意图?
【发布时间】:2012-06-19 14:01:53
【问题描述】:

我已经在这里查看了所有类似的问题,但我一生都无法弄清楚我做错了什么。

我编写了一个尝试启动各种文件的应用程序,类似于文件浏览器。 单击文件时,它会尝试根据其关联的 MIME 类型启动程序,或者显示“选择要启动的应用程序”对话框。

这是我用来启动的代码:

    File file = new File(app.mediaPath() + "/" +_mediaFiles.get(position));

    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);

    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);

这会失败并产生错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///file:/mnt/sdcard/roms/nes/Baseball_simulator.nes }

现在,如果我安装 OI 文件管理器,它会打开而不是引发此错误,然后如果我从其中单击同一文件,它会启动相应的对话框。

我注意到该特定文件的 MIME 类型失败,但其他 MIME 类型(如 .zip)确实返回值。

当 MIME 类型为 null 以调用允许用户选择的对话框时,我是否遗漏了什么?

我尝试了其他启动应用的变体,包括不设置 MIME 类型,只使用.setData,但没有成功。

我想要发生的操作是,用户单击文件,如果它与应用程序启动的应用程序相关联,否则,用户将获得带有应用程序列表的“使用完成操作”对话框。

感谢您的建议。

【问题讨论】:

  • mimetype 字符串中有什么?粘贴到这里
  • 看起来,如果它是具有关联文件的东西,那么它就是正确的 MIME 类型,但如果它是未绑定到应用程序的东西,它会通过 null..

标签: android android-intent file-browser


【解决方案1】:

您可以使用通用意图打开文件,例如 here 提出的这个 sn-p 代码:

private void openFile(File aFile){
    try {
        Intent myIntent = new Intent(android.content.Intent.VIEW_ACTION,
        new ContentURI("file://" + aFile.getAbsolutePath()));
        startActivity(myIntent);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}     

但我通常会看到应用程序在嵌套 if 中检查文件的扩展名,最后尝试使用“text/plain”类型打开文件:

Intent generic = new Intent();
generic.setAction(android.content.Intent.ACTION_VIEW);
generic.setDataAndType(Uri.fromFile(file), "text/plain");     
try {
    startActivity(generic);
    } catch(ActivityNotFoundException e) {
    ...
}     

您可以在this question 或此open source project 中查看完整代码。希望对您有所帮助。

【讨论】:

  • 我尝试了这两种方法,但都没有得到我想要的结果。第一个示例生成相同的异常。第二个尝试仅启动将 text/plain 理解为 mime 类型的应用程序,这样也不起作用。不过还是谢谢。
【解决方案2】:

感谢 Open Intent 的人,我第一次在他们的文件管理器中通过他们的代码错过了答案,这就是我最终得到的结果:

    File file = new File(filePath);
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
    String type = map.getMimeTypeFromExtension(ext);

    if (type == null)
        type = "*/*";

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.fromFile(file);

    intent.setDataAndType(data, type);

    startActivity(intent);

如果您使用 "* / *" 的 mime 类型,而您无法从系统中确定它(它是 null),则会触发相应的选择应用程序对话框。

【讨论】:

  • 警告! - 对于 jpg,mime 类型是 jpeg 而不是 jpg。使用您的代码,如果您提取 jpg 扩展名,它将无法正确设置 mime 类型,因此您将无法查看图像。设置 jpg 图像的 mime 类型时,请务必将 jpg 替换为 jpeg。
  • 感谢它对我的帮助,但在设置 intent.setDataAndType(data, type); 后实际文件不是选择器;文件上传失败。
  • 另外,MimeTypeMap.getFileExtensionFromUrl 不能很好地处理撇号:它会为“World'sHello.docx”返回一个空字符串,但会为“WorldzHello.docx”返回一个 docx。在此处查看正则表达式:github.com/android/platform_frameworks_base/blob/master/core/…
  • 非常有帮助。谢谢!
  • @AndroidDev 在 6.0.1 中似乎已修复。
猜你喜欢
  • 2011-05-20
  • 2014-09-17
  • 1970-01-01
  • 2012-12-08
  • 2014-08-19
  • 2018-06-27
  • 2019-01-28
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多