//File指的是檔案路徑
private void openFile(File file){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//設定intent的Action屬性
intent.setAction(Intent.ACTION_VIEW);
//擷取檔案file的MIME類型
String type = getMIMEType(file);
//設定intent的data和Type屬性。
intent.setDataAndType(Uri.fromFile(file), type);
//跳轉
startActivity(intent);
}
//判斷檔案MimeType的方法
private String getMimeType(File f){
String type="";
String fName = f.getName();
//取得副檔名
String end = fName.substring(fName.lastIndexOf(".")+1 , fName.length()).toLowerCase());
//根據副檔名決定Mime類型
if(end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")){
type = "audio"; }
else if(end.equals("3gp") || end.equals("mp4")){
type = "video";
}
else if(end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")){
type = "image";
}
else if(end.equals("apk")){
//開啟安裝apk程式 , 需要在AndroidManifest中註冊 android.permission.INSTALL_PACKAGES
type = "application/vnd.android.package-archive";
}
return type;
}