小编sto*_*key的帖子

启动文件和MIME类型的意图?

我在这里回顾了所有类似的问题,但我不能为我的生活弄清楚我做错了什么.

我编写了一个试图启动各种文件的应用程序,一种文件浏览器.单击文件时,它会尝试根据其关联的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);
Run Code Online (Sandbox Code Playgroud)

这会失败并生成错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///file:/mnt/sdcard/roms/nes/Baseball_simulator.nes }
Run Code Online (Sandbox Code Playgroud)

现在,如果我安装OI文件管理器,它会打开而不是抛出此错误,然后如果我从其中单击相同的文件,它将启动适当的对话框.

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

我错过了当MIME类型为null时调用允许用户选择的对话框的内容吗?

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

我想要发生的操作是,用户点击文件,如果它与应用程序启动的应用程序相关联,如果没有,则用户获得"使用完整操作"对话框和应用列表.

谢谢你的建议.

android file-browser android-intent

20
推荐指数
1
解决办法
3万
查看次数

标签 统计

android ×1

android-intent ×1

file-browser ×1