这将检测到mime类型并以默认值打开:MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1));
newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context,"No handler for this type of file.", Toast.LENGTH_LONG).show();
}
使用这里函数:private String fileExt(String url) {
if (url.indexOf("?")> -1) {
url = url.substring(0, url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf("."));
if (ext.indexOf("%")> -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/")> -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}