在寫文件管理系統時會用到各種打開不同格式的文件的需求,由於Android系統默認內置了一些可以打開的系統應用,但還是不能滿足需求,比如打開視頻文件、word等,需要安裝相應的播放軟件纔可以使用,這時程序會通過Intent查找可以使用的軟件
實現通過代碼打開一個文件需要2部分,一部分是要獲取到不同文件的後綴,以便根據需求匹配相應的Intent,另一個就是不同格式的文件打開的Intent不同
1、在values目錄下定義後綴數組文件fileendings
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< resources > |
03 |
04 |
< array name = "fileEndingImage" > |
05 |
< item >.png</ item > |
06 |
< item >.gif</ item > |
07 |
< item >.jpg</ item > |
08 |
< item >.jpeg</ item > |
09 |
< item >.bmp</ item > |
10 |
</ array > |
11 |
< array name = "fileEndingAudio" > |
12 |
< item >.mp3</ item > |
13 |
< item >.wav</ item > |
14 |
< item >.ogg</ item > |
15 |
< item >.midi</ item > |
16 |
</ array > |
17 |
< array name = "fileEndingVideo" > |
18 |
< item >.mp4</ item > |
19 |
< item >.rmvb</ item > |
20 |
< item >.avi</ item > |
21 |
< item >.flv</ item > |
22 |
</ array > |
23 |
< array name = "fileEndingPackage" > |
24 |
< item >.jar</ item > |
25 |
< item >.zip</ item > |
26 |
< item >.rar</ item > |
27 |
< item >.gz</ item > |
28 |
< item >.apk</ item > |
29 |
< item >.img</ item > |
30 |
</ array > |
31 |
< array name = "fileEndingWebText" > |
32 |
< item >.htm</ item > |
33 |
< item >.html</ item > |
34 |
< item >.php</ item > |
35 |
< item >.jsp</ item > |
36 |
</ array > |
37 |
< array name = "fileEndingText" > |
38 |
< item >.txt</ item > |
39 |
< item >.java</ item > |
40 |
< item >.c</ item > |
41 |
< item >.cpp</ item > |
42 |
< item >.py</ item > |
43 |
< item >.xml</ item > |
44 |
< item >.json</ item > |
45 |
< item >.log</ item > |
46 |
</ array > |
47 |
< array name = "fileEndingWord" > |
48 |
< item >.doc</ item > |
49 |
< item >.docx</ item > |
50 |
</ array > |
51 |
< array name = "fileEndingExcel" > |
52 |
< item >.xls</ item > |
53 |
< item >.xlsx</ item > |
54 |
</ array > |
55 |
< array name = "fileEndingPPT" > |
56 |
< item >.ppt</ item > |
57 |
< item >.pptx</ item > |
58 |
</ array > |
59 |
< array name = "fileEndingPdf" > |
60 |
< item >.pdf</ item > |
61 |
</ array > |
62 |
63 |
</ resources > |
2、定義OpenFiles工具類,只需傳輸File參數即可,然後通過返回的Intent打開文件
001 |
public class OpenFiles
{ |
002 |
//android獲取一個用於打開HTML文件的intent |
003 |
public static Intent
getHtmlFileIntent(File file) |
004 |
{ |
005 |
Uri
uri = Uri.parse(file.toString()).buildUpon().encodedAuthority( "com.android.htmlfileprovider" ).scheme( "content" ).encodedPath(file.toString()).build(); |
006 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
007 |
intent.setDataAndType(uri, "text/html" ); |
008 |
return intent; |
009 |
} |
010 |
//android獲取一個用於打開圖片文件的intent |
011 |
public static Intent
getImageFileIntent(File file) |
012 |
{ |
013 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
014 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
015 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
016 |
Uri
uri = Uri.fromFile(file); |
017 |
intent.setDataAndType(uri, "image/*" ); |
018 |
return intent; |
019 |
} |
020 |
//android獲取一個用於打開PDF文件的intent |
021 |
public static Intent
getPdfFileIntent(File file) |
022 |
{ |
023 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
024 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
025 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
026 |
Uri
uri = Uri.fromFile(file); |
027 |
intent.setDataAndType(uri, "application/pdf" ); |
028 |
return intent; |
029 |
} |
030 |
//android獲取一個用於打開文本文件的intent |
031 |
public static Intent
getTextFileIntent(File file) |
032 |
{ |
033 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
034 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
035 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
036 |
Uri
uri = Uri.fromFile(file); |
037 |
intent.setDataAndType(uri, "text/plain" ); |
038 |
return intent; |
039 |
} |
040 |
|
041 |
//android獲取一個用於打開音頻文件的intent |
042 |
public static Intent
getAudioFileIntent(File file) |
043 |
{ |
044 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
045 |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
046 |
intent.putExtra( "oneshot" , 0 ); |
047 |
intent.putExtra( "configchange" , 0 ); |
048 |
Uri
uri = Uri.fromFile(file); |
049 |
intent.setDataAndType(uri, "audio/*" ); |
050 |
return intent; |
051 |
} |
052 |
//android獲取一個用於打開視頻文件的intent |
053 |
public static Intent
getVideoFileIntent(File file) |
054 |
{ |
055 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
056 |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
057 |
intent.putExtra( "oneshot" , 0 ); |
058 |
intent.putExtra( "configchange" , 0 ); |
059 |
Uri
uri = Uri.fromFile(file); |
060 |
intent.setDataAndType(uri, "video/*" ); |
061 |
return intent; |
062 |
} |
063 |
|
064 |
|
065 |
//android獲取一個用於打開CHM文件的intent |
066 |
public static Intent
getChmFileIntent(File file) |
067 |
{ |
068 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
069 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
070 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
071 |
Uri
uri = Uri.fromFile(file); |
072 |
intent.setDataAndType(uri, "application/x-chm" ); |
073 |
return intent; |
074 |
} |
075 |
|
076 |
|
077 |
//android獲取一個用於打開Word文件的intent |
078 |
public static Intent
getWordFileIntent(File file) |
079 |
{ |
080 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
081 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
082 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
083 |
Uri
uri = Uri.fromFile(file); |
084 |
intent.setDataAndType(uri, "application/msword" ); |
085 |
return intent; |
086 |
} |
087 |
//android獲取一個用於打開Excel文件的intent |
088 |
public static Intent
getExcelFileIntent(File file) |
089 |
{ |
090 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
091 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
092 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
093 |
Uri
uri = Uri.fromFile(file); |
094 |
intent.setDataAndType(uri, "application/vnd.ms-excel" ); |
095 |
return intent; |
096 |
} |
097 |
//android獲取一個用於打開PPT文件的intent |
098 |
public static Intent
getPPTFileIntent(File file) |
099 |
{ |
100 |
Intent
intent = new Intent( "android.intent.action.VIEW" ); |
101 |
intent.addCategory( "android.intent.category.DEFAULT" ); |
102 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
103 |
Uri
uri = Uri.fromFile(file); |
104 |
intent.setDataAndType(uri, "application/vnd.ms-powerpoint" ); |
105 |
return intent; |
106 |
} |
107 |
//android獲取一個用於打開apk文件的intent |
108 |
public static Intent
getApkFileIntent(File file) |
109 |
{ |
110 |
Intent
intent = new Intent(); |
111 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
112 |
intent.setAction(android.content.Intent.ACTION_VIEW); |
113 |
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive" ); |
114 |
return intent; |
115 |
} |
116 |
|
117 |
|
118 |
} |
3、定義用於檢查要打開的文件的後綴是否在遍歷後綴數組中
1 |
private boolean checkEndsWithInStringArray(String
checkItsEnd, |
2 |
String[]
fileEndings){ |
3 |
for (String
aEnd : fileEndings){ |
4 |
if (checkItsEnd.endsWith(aEnd)) |
5 |
return true ; |
6 |
} |
7 |
return false ; |
8 |
} |
4、通過調用OpenFiles類返回的Intent,打開相應的文件
01 |
if (currentPath!= null &¤tPath.isFile()) |
02 |
{ |
03 |
String
fileName = currentPath.toString(); |
04 |
Intent
intent; |
05 |
if (checkEndsWithInStringArray(fileName,
getResources(). |
06 |
getStringArray(R.array.fileEndingImage))){ |
07 |
intent
= OpenFiles.getImageFileIntent(currentPath); |
08 |
startActivity(intent); |
09 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
10 |
getStringArray(R.array.fileEndingWebText))){ |
11 |
intent
= OpenFiles.getHtmlFileIntent(currentPath); |
12 |
startActivity(intent); |
13 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
14 |
getStringArray(R.array.fileEndingPackage))){ |
15 |
intent
= OpenFiles.getApkFileIntent(currentPath); |
16 |
startActivity(intent); |
17 |
18 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
19 |
getStringArray(R.array.fileEndingAudio))){ |
20 |
intent
= OpenFiles.getAudioFileIntent(currentPath); |
21 |
startActivity(intent); |
22 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
23 |
getStringArray(R.array.fileEndingVideo))){ |
24 |
intent
= OpenFiles.getVideoFileIntent(currentPath); |
25 |
startActivity(intent); |
26 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
27 |
getStringArray(R.array.fileEndingText))){ |
28 |
intent
= OpenFiles.getTextFileIntent(currentPath); |
29 |
startActivity(intent); |
30 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
31 |
getStringArray(R.array.fileEndingPdf))){ |
32 |
intent
= OpenFiles.getPdfFileIntent(currentPath); |
33 |
startActivity(intent); |
34 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
35 |
getStringArray(R.array.fileEndingWord))){ |
36 |
intent
= OpenFiles.getWordFileIntent(currentPath); |
37 |
startActivity(intent); |
38 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
39 |
getStringArray(R.array.fileEndingExcel))){ |
40 |
intent
= OpenFiles.getExcelFileIntent(currentPath); |
41 |
startActivity(intent); |
42 |
} else if (checkEndsWithInStringArray(fileName,
getResources(). |
43 |
getStringArray(R.array.fileEndingPPT))){ |
44 |
intent
= OpenFiles.getPPTFileIntent(currentPath); |
45 |
startActivity(intent); |
46 |
} else |
47 |
{ |
48 |
showMessage( "無法打開,請安裝相應的軟件!" ); |
49 |
} |
50 |
} else |
51 |
{ |
52 |
showMessage( "對不起,這不是文件!" ); |
53 |
} |