Android編程開發(fā)之打開文件的Intent及使用方法
本文實(shí)例講述了Android編程開發(fā)之打開文件的Intent及使用方法。分享給大家供大家參考,具體如下:
在寫文件管理系統(tǒng)時(shí)會(huì)用到各種打開不同格式的文件的需求,由于Android系統(tǒng)默認(rèn)內(nèi)置了一些可以打開的系統(tǒng)應(yīng)用,但還是不能滿足需求,比如打開視頻文件、word等,需要安裝相應(yīng)的播放軟件才可以使用,這時(shí)程序會(huì)通過Intent查找可以使用的軟件實(shí)現(xiàn)通過代碼打開一個(gè)文件需要2部分,一部分是要獲取到不同文件的后綴,以便根據(jù)需求匹配相應(yīng)的Intent,另一個(gè)就是不同格式的文件打開的Intent不同
1、在values目錄下定義后綴數(shù)組文件fileendings
<?xml version="1.0" encoding="utf-8"?> <resources> <array name="fileEndingImage"> <item>.png</item> <item>.gif</item> <item>.jpg</item> <item>.jpeg</item> <item>.bmp</item> </array> <array name="fileEndingAudio"> <item>.mp3</item> <item>.wav</item> <item>.ogg</item> <item>.midi</item> </array> <array name="fileEndingVideo"> <item>.mp4</item> <item>.rmvb</item> <item>.avi</item> <item>.flv</item> </array> <array name="fileEndingPackage"> <item>.jar</item> <item>.zip</item> <item>.rar</item> <item>.gz</item> <item>.apk</item> <item>.img</item> </array> <array name="fileEndingWebText"> <item>.htm</item> <item>.html</item> <item>.php</item> <item>.jsp</item> </array> <array name="fileEndingText"> <item>.txt</item> <item>.java</item> <item>.c</item> <item>.cpp</item> <item>.py</item> <item>.xml</item> <item>.json</item> <item>.log</item> </array> <array name="fileEndingWord"> <item>.doc</item> <item>.docx</item> </array> <array name="fileEndingExcel"> <item>.xls</item> <item>.xlsx</item> </array> <array name="fileEndingPPT"> <item>.ppt</item> <item>.pptx</item> </array> <array name="fileEndingPdf"> <item>.p<?xml version="1.0" encoding="utf-8"?> <resources> <array name="fileEndingImage"> <item>.png</item> <item>.gif</item> <item>.jpg</item> <item>.jpeg</item> <item>.bmp</item> </array> <array name="fileEndingAudio"> <item>.mp3</item> <item>.wav</item> <item>.ogg</item> <item>.midi</item> </array> <array name="fileEndingVideo"> <item>.mp4</item> <item>.rmvb</item> <item>.avi</item> <item>.flv</item> </array> <array name="fileEndingPackage"> <item>.jar</item> <item>.zip</item> <item>.rar</item> <item>.gz</item> <item>.apk</item> <item>.img</item> </array> <array name="fileEndingWebText"> <item>.htm</item> <item>.html</item> <item>.php</item> <item>.jsp</item> </array> <array name="fileEndingText"> <item>.txt</item> <item>.java</item> <item>.c</item> <item>.cpp</item> <item>.py</item> <item>.xml</item> <item>.json</item> <item>.log</item> </array> <array name="fileEndingWord"> <item>.doc</item> <item>.docx</item> </array> <array name="fileEndingExcel"> <item>.xls</item> <item>.xlsx</item> </array> <array name="fileEndingPPT"> <item>.ppt</item> <item>.pptx</item> </array> <array name="fileEndingPdf"> <item>.pdf</item> </array> </resources>df</item> </array> </resources>
2、定義OpenFiles工具類,只需傳輸File參數(shù)即可,然后通過返回的Intent打開文件
public class OpenFiles {
//android獲取一個(gè)用于打開HTML文件的intent
public static Intent getHtmlFileIntent(File file)
{
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
//android獲取一個(gè)用于打開圖片文件的intent
public static Intent getImageFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "image/*");
return intent;
}
//android獲取一個(gè)用于打開PDF文件的intent
public static Intent getPdfFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "application/pdf");
return intent;
}
//android獲取一個(gè)用于打開文本文件的intent
public static Intent getTextFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "text/plain");
return intent;
}
//android獲取一個(gè)用于打開音頻文件的intent
public static Intent getAudioFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "audio/*");
return intent;
}
//android獲取一個(gè)用于打開視頻文件的intent
public static Intent getVideoFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "video/*");
return intent;
}
//android獲取一個(gè)用于打開CHM文件的intent
public static Intent getChmFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
//android獲取一個(gè)用于打開Word文件的intent
public static Intent getWordFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "application/msword");
return intent;
}
//android獲取一個(gè)用于打開Excel文件的intent
public static Intent getExcelFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
//android獲取一個(gè)用于打開PPT文件的intent
public static Intent getPPTFileIntent(File file)
{
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(file);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
//android獲取一個(gè)用于打開apk文件的intent
public static Intent getApkFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
return intent;
}
}
3、定義用于檢查要打開的文件的后綴是否在遍歷后綴數(shù)組中
private boolean checkEndsWithInStringArray(String checkItsEnd,
String[] fileEndings){
for(String aEnd : fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
4、通過調(diào)用OpenFiles類返回的Intent,打開相應(yīng)的文件
if(currentPath!=null&¤tPath.isFile())
{
String fileName = currentPath.toString();
Intent intent;
if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingImage))){
intent = OpenFiles.getImageFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingWebText))){
intent = OpenFiles.getHtmlFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPackage))){
intent = OpenFiles.getApkFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingAudio))){
intent = OpenFiles.getAudioFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingVideo))){
intent = OpenFiles.getVideoFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingText))){
intent = OpenFiles.getTextFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPdf))){
intent = OpenFiles.getPdfFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingWord))){
intent = OpenFiles.getWordFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingExcel))){
intent = OpenFiles.getExcelFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPPT))){
intent = OpenFiles.getPPTFileIntent(currentPath);
startActivity(intent);
}else
{
showMessage("無法打開,請(qǐng)安裝相應(yīng)的軟件!");
}
}else
{
showMessage("對(duì)不起,這不是文件!");
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)打開各種文件的intent方法小結(jié)
- Android開發(fā)實(shí)現(xiàn)的Intent跳轉(zhuǎn)工具類實(shí)例
- Android使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android使用Intent獲取聯(lián)系人信息
- Android Intent的幾種用法詳細(xì)解析
- Android Intent啟動(dòng)別的應(yīng)用實(shí)現(xiàn)方法
- 詳解Android中Intent的使用方法
- 詳解Android應(yīng)用開發(fā)中Intent的作用及使用方法
- Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法
- Android開發(fā)中使用Intent打開第三方應(yīng)用及驗(yàn)證可用性的方法詳解
相關(guān)文章
Android自定義動(dòng)畫根據(jù)控件Y軸旋轉(zhuǎn)動(dòng)畫(仿紅包)
這篇文章主要介紹了Android自定義動(dòng)畫根據(jù)控件Y軸旋轉(zhuǎn)動(dòng)畫(仿紅包),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Android DSelectorBryant 單選滾動(dòng)選擇器的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android DSelectorBryant 單選滾動(dòng)選擇器的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android中View.post和Handler.post的關(guān)系
這篇文章主要介紹了Android中View.post和Handler.post的關(guān)系,View.post和Handler.post是Android開發(fā)中經(jīng)常使用到的兩個(gè)”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細(xì)分析需要的小伙伴可以參考一下2022-06-06
Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android補(bǔ)間動(dòng)畫的原理、功能實(shí)現(xiàn)與布局相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android獲得所有存儲(chǔ)設(shè)備位置的最佳方法
今天小編就為大家分享一篇Android獲得所有存儲(chǔ)設(shè)備位置的最佳方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Flutter實(shí)現(xiàn)自定義搜索框AppBar的示例代碼
開發(fā)中,頁面頭部為搜索樣式的設(shè)計(jì)非常常見,為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個(gè)搜索框,感興趣的可以了解一下2022-04-04
詳解Android中App的啟動(dòng)界面Splash的編寫方法
這篇文章主要介紹了Android中App的啟動(dòng)界面Splash的編寫方法,需要的朋友可以參考下2016-02-02

