启动一个意向文件和MIME类型?意向、类型、文件、MIME

2023-09-12 01:14:02 作者:花间一壶酒

我已经查看了所有类似的问题在这里,但我不能为我的生命找出我做错了。

I've reviewed all the similar questions here, but I can't for the life of me figure out what I'm doing wrong.

我已经写了一种尝试推出各种文件,排序文件浏览器应用程序。 当点击一个文件,它会尝试推出基于其关联的MIME类型的程序或presents选择应用程序启动对话框。

I've written an application that tries to launch various files, sort of a file browser. When a file is clicked, it tries to launch the program based on its associated MIME type or it presents the "Choose Application to Launch" dialog.

这里的code我使用启动:

Here's the code I'm using to launch:

    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文件管理器,例如,它会打开,而不是这个错误被抛出,然后如果我从内它,它会启动选择恰当的对话框中单击相同的文件。

Now if I install OI File Manager for instance, it opens instead of this error being thrown, and then if I click the same file from within in it, it launches the approriate dialogs.

我已经注意到,MIME类型特定的文件失败,但像的.zip 其他MIME类型做返回值。

I have noticed that the MIME type for that particular file fails, but other mime types like .zip do return values.

我缺少的东西,当MIME类型为null调用一个对话框,让用户选择?

Am I missing something that when the MIME type is null to call a dialog that lets the user select?

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

I've tried other variations of launching the app, including not setting the MIME type and only using .setData with no success.

我希望发生的动作,用户点击一个文件,如果它与应用程序启动,如果没有,用户使用完整的行动对话框,应用程序的列表,获取应用程序相关联。

The action I want to happen is, a user clicks a file, if it's associated with an application that app launches, if not, the user gets the "Complete action using" dialog with a list of apps.

感谢您的任何建议。

推荐答案

好于开放式意图家伙,谢谢,我错过了答案第一时间通过自己的code在他们的文件管理器,这是我结束了:

Well thanks to the Open Intent guys, I missed the answer the first time through their code in their file manager, here's what I ended up with:

    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类型* / *当你无法从系统中确定它(这是)它激发适当的选择应用程序的对话框。

If you use a mime type of "* / *" when you can't determine it from the system(it is null) it fires the appropriate select application dialog.