» 首頁 » 討論區 » Android程式設計 »開啓檔案問題

開啓檔案問題

發表人: hktsang
積分: 16
發表時間: 2011-10-11 11:56:33
本人寫了一個程式是當按下file name時開啓相應的程式執行, 以下是我的code, 但只能執部份程式如(mp3, mp4, png, jpg)其他程式如(msword, pdf, ms-excel...)不能執行, 請指教。

[sea:javaCode]
private void openFile(File f)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String mExt = MimeTypeMap.getFileExtensionFromUrl(f.getName());
String mMimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mExt);
String type = mMimetype;
type += "/*";
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}

[/sea]
發表人: Seachaos
積分: 2432
發表時間: 2011-10-11 22:09:10
目前看來有問題的二個地方可能是
1. 你的Mini Type加了*
以PDF為例,他是 application/pdf
PPT是application/vnd.ms-powerpoint
所以有可能是不正確的Mini Type造成的

2. 你的File Path可能不正確
可以用Log + DDMS來看一下是否正確
發表人: hktsang
積分: 16
發表時間: 2011-10-13 14:58:00
我已檢查過"String mMimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mExt);" 中的mMimetype內的字串是正確的。如:.doc == Application/msword, .pdf == Application/pdf etc.
但在手機上執行就開不到。
發表人: Seachaos
積分: 2432
發表時間: 2011-10-15 01:05:37
Hi,
這是我寫的Android開啟PDF方法
你可以試看看可不可以 (我測試我的手機是可以的)
可以的話就可以修改成word版本


[sea:javaCode]
File fe = new File(fileName);
if(fe.isFile()){
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(fe);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}else{
message("Not found PDF");
}
[/sea]
發表人: hktsang
積分: 16
發表時間: 2011-10-18 14:57:15
謝謝, 但原來不須要+ "/*"在"application/pdf"後面?
發表人: Seachaos
積分: 2432
發表時間: 2011-10-18 23:24:25
應該是不用
因我目前用到現在還沒有看過有mime type後面有 /* 的(也許某些格式或特殊用途有吧)