2014-09-01 170 views
0

我编写了自己的文件资源管理器,现在我想开始实现打开各种文件的功能。我已经实现了MIME类型和所有其他的东西,但我怎么能解析特定的选择文件到特定的“查看器活动”?我知道通过使用意图,但如何在该查看器应用程序中接收它并使用它。非常感谢球员:)从自己的文件资源管理器打开文件

回答

0

像这样的东西应该工作。将文件更改为任何你想要的。

File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4"); 
int index = file.getName().lastIndexOf(".") + 1; 
String extension = null; 
if (index > 0) { 
    extension = file.getName().substring(index); 
} 
Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
Uri u = Uri.parse("file://" + file.getAbsolutePath()); 
String mimeType = null; 
if (extension != null) { 
    mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
} 
if (mimeType == null) { 
    mimeType = "*/*"; 
} 
intent.setDataAndType(u, mimeType); 
try { 
    startActivity(Intent.createChooser(intent, "Open with...")); 
} catch (ActivityNotFoundException e) { 
    // handle the exception 
} 
+0

对不起,错误的问题。我问了如何从接受意图的活动中处理它 – Trabefi 2014-09-01 09:49:26

+0

啊,我误解了这个问题。像这样的应该工作:http://stackoverflow.com/questions/1733195/android-intent-filter-for-a-particular-file-extension – 2014-09-01 22:39:56

相关问题