Android 启动文件和 MIME 类型的意图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11068648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launching an intent for file and MIME type?
提问by stonedonkey
I've reviewed all the similar questions here, but I can't for the life of me figure out what I'm doing wrong.
我已经在这里回顾了所有类似的问题,但我一生都无法弄清楚我做错了什么。
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.
我编写了一个尝试启动各种文件的应用程序,有点像文件浏览器。单击文件时,它会尝试根据其关联的 MIME 类型启动程序,或者显示“选择要启动的应用程序”对话框。
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);
This fails and generates the error:
这失败并生成错误:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///file:/mnt/sdcard/roms/nes/Baseball_simulator.nes }
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.
现在,如果我安装 OI 文件管理器,它会打开而不是抛出此错误,然后如果我从其中单击同一个文件,它会启动相应的对话框。
I have noticed that the MIME type for that particular file fails, but other mime types like .zip
do return values.
我注意到该特定文件的 MIME 类型失败,但其他 MIME 类型(如.zip
返回值)。
Am I missing something that when the MIME type is null to call a dialog that lets the user select?
我是否错过了当 MIME 类型为空时调用允许用户选择的对话框的内容?
I've tried other variations of launching the app, including not setting the MIME type and only using .setData
with no success.
我尝试了启动应用程序的其他变体,包括不设置 MIME 类型和仅使用但.setData
没有成功。
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.
我想要发生的操作是,用户单击一个文件,如果它与应用程序启动的应用程序相关联,否则,用户将获得带有应用程序列表的“使用完成操作”对话框。
Thanks for any advice.
感谢您的任何建议。
回答by stonedonkey
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:
好吧,多亏了 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);
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.
如果您使用 mime 类型,"* / *"
当您无法从系统(它是null
)确定它时,它会触发相应的选择应用程序对话框。
回答by hasanghaforian
You can use generic intents to open files,like this snippet code that is proposed here:
您可以使用通用意图来打开文件,就像这里提出的这个片段代码:
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();
}
}
But I usually see that Applications checks the file's extension in nested if and finally try to open file with "text/plain" type:
但我通常看到应用程序在嵌套中检查文件的扩展名,最后尝试使用“ 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) {
...
}
You can see complete code in this questionor in this open source project. I hope this help you.