服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程开发之打开文件的Intent及使用方法

Android编程开发之打开文件的Intent及使用方法

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

这篇文章主要介绍了Android编程开发之打开文件的Intent及使用方法,已实例形式分析了Android打开文件Intent的相关布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程开发之打开文件Intent及使用方法。分享给大家供大家参考,具体如下:

在写文件管理系统时会用到各种打开不同格式的文件的需求,由于Android系统默认内置了一些可以打开的系统应用,但还是不能满足需求,比如打开视频文件、word等,需要安装相应的播放软件才可以使用,这时程序会通过Intent查找可以使用的软件实现通过代码打开一个文件需要2部分,一部分是要获取到不同文件的后缀,以便根据需求匹配相应的Intent,另一个就是不同格式的文件打开的Intent不同

1、在values目录下定义后缀数组文件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参数即可,然后通过返回的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获取一个用于打开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获取一个用于打开图片文件的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获取一个用于打开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获取一个用于打开文本文件的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获取一个用于打开音频文件的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获取一个用于打开视频文件的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获取一个用于打开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获取一个用于打开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获取一个用于打开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获取一个用于打开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获取一个用于打开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、定义用于检查要打开的文件的后缀是否在遍历后缀数组中

?
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、通过调用OpenFiles类返回的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
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("无法打开,请安装相应的软件!");
}
}else
{
showMessage("对不起,这不是文件!");
}

希望本文所述对大家Android程序设计有所帮助。

延伸 · 阅读

精彩推荐