嗨,我有一个媒体文件 URI,我想确定 mimetype 并以通用方式调用适当的活动,以下是我的代码。
mediaUri = Uri.parse(filePath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(Intent.ACTION_VIEW);
viewMediaIntent.setDataAndType(mediaUri,mimeType);
viewMediaIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(viewMediaIntent);
现在的问题是很少有文件 uris 不起作用。但是如果将相同的 uri 转换为 http uri 并从浏览器加载它们工作正常。或任何其他应用程序都能够加载它们,
不起作用的示例 URI
file:///storage/emulated/0/Samsung/Image/001.JPG
file:///storage/emulated/0/Samsung/Music/Over the horizon.mp3
错误:
09-30 12:07:44.531: I/System.out(26771): Item Selected: file:///storage/emulated/0/Samsung/Image/001.JPG`
09-30 12:07:44.531: I/System.out(26771): mime type: null`
09-30 12:07:44.534: D/AndroidRuntime(26771): Shutting down VM`
09-30 12:07:44.534: W/dalvikvm(26771): threadid=1: thread exiting with uncaught exception (group=0x41e50ac8)`
09-30 12:07:44.545: E/AndroidRuntime(26771): FATAL EXCEPTION: main`
09-30 12:07:44.545: E/AndroidRuntime(26771): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Samsung/Image/001.JPG flg=0x14000000 }`
09-30 12:07:44.545: E/AndroidRuntime(26771): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)`
09-30 12:07:44.545: E/AndroidRuntime(26771): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)`