国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android編程開發(fā)之打開文件的Intent及使用方法

Android編程開發(fā)之打開文件的Intent及使用方法

2021-04-05 15:11非著名程序員 Android

這篇文章主要介紹了Android編程開發(fā)之打開文件的Intent及使用方法,已實(shí)例形式分析了Android打開文件Intent的相關(guān)布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android編程開發(fā)之打開文件Intent及使用方法。分享給大家供大家參考,具體如下:

在寫文件管理系統(tǒng)時(shí)會用到各種打開不同格式的文件的需求,由于Android系統(tǒng)默認(rèn)內(nèi)置了一些可以打開的系統(tǒng)應(yīng)用,但還是不能滿足需求,比如打開視頻文件、word等,需要安裝相應(yīng)的播放軟件才可以使用,這時(shí)程序會通過Intent查找可以使用的軟件實(shí)現(xiàn)通過代碼打開一個(gè)文件需要2部分,一部分是要獲取到不同文件的后綴,以便根據(jù)需求匹配相應(yīng)的Intent,另一個(gè)就是不同格式的文件打開的Intent不同

1、在values目錄下定義后綴數(shù)組文件fileendings

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?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打開文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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ù)組中

?
1
2
3
4
5
6
7
8
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)的文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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("無法打開,請安裝相應(yīng)的軟件!");
}
}else
{
showMessage("對不起,這不是文件!");
}

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲综合无码一区二区 | 成人午夜视频在线观看 | 精品一区二区不卡 | 依人成人综合网 | 国产小视频在线播放 | 午夜精品影院 | 99热热热| 99国产精品 | 中文字幕69av| 午夜精品一区二区三区在线视频 | 亚洲免费看片 | 在线国产视频 | 欧美日韩一区二区三区免费视频 | 精品一区二区三区免费毛片爱 | 久久久国产一区二区三区 | 国产露脸国语对白在线 | 亚洲精品国产综合99久久夜夜嗨 | 国产亚洲精品精品国产亚洲综合 | 中文字幕在线免费视频 | 久久国产精品久久久久久 | 欧美精品福利 | 免费黄色在线观看 | 国产真实精品久久二三区 | 成人免费视频视频在线观看 免费 | 成人免费视频网站在线观看 | 亚洲精品乱码久久久久久麻豆不卡 | 日韩亚洲 | 国产毛片毛片 | 91精品国产综合久久久久 | 毛片网站大全 | 午夜精品一区二区三区在线视频 | 国产精品2区 | 超碰毛片 | 精品国产欧美一区二区 | 亚洲国产综合在线观看 | 亚洲精品成a人在线 | 日韩精品99 | 欧美99 | 精品一区二区久久久久黄大片 | 国产精品成av人在线视午夜片 | 人一级毛片|