2014-09-01 135 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

相關問題