介紹
本篇用于介紹Android中如何打開各種類型的文件,如:音樂文件(mp3尿孔、wav俊柔、ogg等)、視頻文件(3gp活合、mp4等)雏婶、圖片文件(jpg、png白指、gif等)留晚、安裝包(apk)、文檔(txt告嘲、doc错维、ppt、pdf橄唬、xls等)需五,已經(jīng)封裝成工具類OpenFileUtils,末尾有源碼地址轧坎。
注意
打開音樂宏邮、視頻、圖片缸血、文檔等文件是需要有讀取SD卡的權(quán)限的蜜氨,如果是6.0以下的系統(tǒng),則直接在清單文件中聲明SD卡讀取權(quán)限即可捎泻;如果是6.0或以上飒炎,則需要?jiǎng)討B(tài)申請(qǐng)權(quán)限。
為了兼容Android7.0笆豁,獲取文件Uri需要使用到FileProvider郎汪,由于本篇只是介紹如何打開文件赤赊,就不再介紹如何使用FileProvider,不清楚的同學(xué)可以查看我寫過的一篇關(guān)于Android7.0適配的博客煞赢,里面有介紹到該內(nèi)容抛计,博客地址:
http://blog.csdn.net/chay_chan/article/details/57083383
OpenFileUtils封裝好的獲取文件Uri的方法getUri()的代碼為:
/**
* 獲取對(duì)應(yīng)文件的Uri
* @param intent 相應(yīng)的Intent
* @param file 文件對(duì)象
* @return
*/
private static Uri getUri(Intent intent, File file) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//判斷版本是否在7.0以上
uri =
FileProvider.getUriForFile(mContext,
mContext.getPackageName() + ".fileprovider",
file);
//添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
OpenFileUtils的使用
OpenFileUtils是封裝好的用于打開本機(jī)各類文件的工具類,使用方式為:OpenFileUtils.getInstance(Context context).openFile(String filePath)照筑。
各種文件的類型的DataType
DataType用于指定Intent中的數(shù)據(jù)類型吹截,不同類型的文件有不同的DataType,傳入相應(yīng)類型的DataType凝危,系統(tǒng)會(huì)搜尋可以打開該文件的軟件波俄,比如傳入視頻的DataType,即"video/*"蛾默,那么系統(tǒng)將會(huì)搜尋本機(jī)中可以播放視頻的軟件懦铺,如果沒有安裝其他視頻播放軟件,則系統(tǒng)會(huì)默認(rèn)使用自帶的播放視頻軟件支鸡,如果你還安裝了其他視頻播放軟件阀趴,如手機(jī)QQ影音,那么就會(huì)彈出選擇框讓你選擇使用哪個(gè)軟件打開苍匆。
下面第一個(gè)DataType未指定明確的文件類型,那么此時(shí)系統(tǒng)會(huì)彈出所有可以打開文件的軟件棚菊,需要用戶自己判斷使用哪個(gè)軟件才可以打開浸踩。
/**聲明各種類型文件的dataType**/
private static final String DATA_TYPE_ALL = "*/*";//未指定明確的文件類型,不能使用精確類型的工具打開统求,需要用戶選擇
private static final String DATA_TYPE_APK = "application/vnd.android.package-archive";
private static final String DATA_TYPE_VIDEO = "video/*";
private static final String DATA_TYPE_AUDIO = "audio/*";
private static final String DATA_TYPE_HTML = "text/html";
private static final String DATA_TYPE_IMAGE = "image/*";
private static final String DATA_TYPE_PPT = "application/vnd.ms-powerpoint";
private static final String DATA_TYPE_EXCEL = "application/vnd.ms-excel";
private static final String DATA_TYPE_WORD = "application/msword";
private static final String DATA_TYPE_CHM = "application/x-chm";
private static final String DATA_TYPE_TXT = "text/plain";
private static final String DATA_TYPE_PDF = "application/pdf";
打開文件的方法
/**
* 打開文件
* @param filePath 文件的全路徑检碗,包括到文件名
*/
private static void openFile(String filePath) {
File file = new File(filePath);
if (!file.exists()){
//如果文件不存在
Toast.makeText(mContext, "打開失敗,原因:文件已經(jīng)被移動(dòng)或者刪除", Toast.LENGTH_SHORT).show();
return;
}
/* 取得擴(kuò)展名 */
String end = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase(Locale.getDefault());
/* 依擴(kuò)展名的類型決定MimeType */
Intent intent = null;
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
intent = generateVideoAudioIntent(filePath,DATA_TYPE_AUDIO);
} else if (end.equals("3gp") || end.equals("mp4")) {
intent = generateVideoAudioIntent(filePath,DATA_TYPE_VIDEO);
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) {
intent = generateCommonIntent(filePath,DATA_TYPE_IMAGE);
} else if (end.equals("apk")) {
intent = generateCommonIntent(filePath,DATA_TYPE_APK);
}else if (end.equals("html") || end.equals("htm")){
intent = getHtmlFileIntent(filePath);
} else if (end.equals("ppt")) {
intent = generateCommonIntent(filePath,DATA_TYPE_PPT);
} else if (end.equals("xls")) {
intent = generateCommonIntent(filePath,DATA_TYPE_EXCEL);
} else if (end.equals("doc")) {
intent = generateCommonIntent(filePath,DATA_TYPE_WORD);
} else if (end.equals("pdf")) {
intent = generateCommonIntent(filePath,DATA_TYPE_PDF);
} else if (end.equals("chm")) {
intent = generateCommonIntent(filePath,DATA_TYPE_CHM);
} else if (end.equals("txt")) {
intent = generateCommonIntent(filePath, DATA_TYPE_TXT);
} else {
intent = generateCommonIntent(filePath,DATA_TYPE_ALL);
}
mContext.startActivity(intent);
}
打開文件的處理步驟如下:
- 截取得到文件的后綴名
- 根據(jù)后綴名判斷對(duì)應(yīng)的文件屬于哪種DataType码邻,調(diào)用對(duì)應(yīng)產(chǎn)生封裝好的intent的方法折剃,獲取到intent;
- 調(diào)用startActivity()方法像屋,傳入intent
其中怕犁,generateVideoAudioIntent(String filePath, String dataType)是產(chǎn)生打開視頻和音頻類型文件的Intent的方法,代碼如下:
/**
* 產(chǎn)生打開視頻或音頻的Intent
* @param filePath 文件路徑
* @param dataType 文件類型
* @return
*/
private static Intent generateVideoAudioIntent(String filePath, String dataType){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
File file = new File(filePath);
intent.setDataAndType(getUri(intent,file), dataType);
return intent;
}
generateHtmlFileIntent()是產(chǎn)生打開網(wǎng)頁(yè)類型文件的Intent的方法己莺,代碼如下:
/**
* 產(chǎn)生打開網(wǎng)頁(yè)文件的Intent
* @param filePath 文件路徑
* @return
*/
private static Intent generateHtmlFileIntent(String filePath) {
Uri uri = Uri.parse(filePath)
.buildUpon()
.encodedAuthority("com.android.htmlfileprovider")
.scheme("content")
.encodedPath(filePath)
.build();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, DATA_TYPE_HTML);
return intent;
}
打開其他類型文件的方法generateCommonIntent(String filePath, String dataType)奏甫,代碼如下:
/**
* 產(chǎn)生除了視頻、音頻凌受、網(wǎng)頁(yè)文件外阵子,打開其他類型文件的Intent
* @param filePath 文件路徑
* @param dataType 文件類型
* @return
*/
private static Intent generateCommonIntent(String filePath, String dataType) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
File file = new File(filePath);
Uri uri = getUri(intent, file);
intent.setDataAndType(uri, dataType);
return intent;
}
OpenFileUtils的源碼,可以點(diǎn)擊下面的網(wǎng)址跳轉(zhuǎn)查看和下載: