使用Android系统支持的或者第三方APK打开各种类型的文件
原理:根据文件名后缀,判断出文件的类型,如PPT/TXT/PDF等类型,设置Intent的Data and Type
FileHelper源代码
package com.example.common;
import java.io.File;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
public class FileHelper {
private static final String TAG = "FileHelper";
public static void openFile(String filePath, Context context){
if(TextUtils.isEmpty(filePath)||null == context){
Log.e(TAG, "--openFile--filePath is empty or context is null, retun");
return;
}
File file = new File(filePath);
if(null != file && file.exists()){
String fileName = file.getName();
Log.d(TAG, "--file is exist, filePath:"+filePath+"--fileName:"+fileName);
String fileType = "*/*";
String end = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||
end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
fileType = "audio/*";
}
else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||
end.equals("jpeg")||end.equals("bmp")||end.equals(".pic")){
fileType = "image/*";
}
else if(end.equals("txt")){
fileType = "text/plain";
}
else if(end.equals("pdf")){
fileType = "application/pdf";
}
else if(end.equals("doc")){
fileType = "application/msword";
}
else if(end.equals("xls")){
fileType = "application/vnd.ms-excel";
}
else if(end.equals("ppt")){
fileType = "application/vnd.ms-powerpoint";
}
// 可以扩展自己需要的类型
Log.d(TAG, "--fileType:"+fileType);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), fileType);
try {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "--can not startActivity--");
Toast.makeText(context, "暂不支持打开该文件", Toast.LENGTH_SHORT).show();
}
}
}
}
注意点
1. 在使用context.startActivity(intent);使,要加上try catch,不然若不存在打开该类型的应用,则会crash
异常如下:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/1.ppt typ=application/vnd.ms-powerpoint flg=0x10000000 }
- 该工具类并未判断文件是否真的类型和后缀一致。如“.pic”结尾的文本文件,用系统“图库”打开可能是黑的或者出现异常。
异常如下:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.android.gallery3d.data.Path.getPrefix()' on a null object reference
07-07 16:08:20.672: E/AndroidRuntime(7816): at com.android.gallery3d.data.DataManager.getDefaultSetOf(DataManager.java:278)
07-07 16:08:20.672: E/AndroidRuntime(7816): at com.android.gallery3d.app.GalleryActivity.startViewAction(GalleryActivity.java:206)
07-07 16:08:20.672: E/AndroidRuntime(7816): at com.android.gallery3d.app.GalleryActivity.initializeByIntent(GalleryActivity.java:96)
07-07 16:08:20.672: E/AndroidRuntime(7816): at com.android.gallery3d.app.GalleryActivity.onCreate(GalleryActivity.java:73)
07-07 16:08:20.672: E/AndroidRuntime(7816): at android.app.Activity.performCreate(Activity.java:5990)
07-07 16:08:20.672: E/AndroidRuntime(7816): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
07-07 16:08:20.672: E/AndroidRuntime(7816): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
07-07 16:08:20.672: E/AndroidRuntime(7816): ... 10 more
07-07 16:08:20.702: W/ActivityManager(931): Force finishing activity 1 com.android.gallery3d/.app.GalleryActivity