具体代码实现片段是:
-
<pre name=
“code”
class
=
“java”
>Intent intent =
new
Intent(Intent.ACTION_VIEW);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(Uri.fromFile(
new
File(filePath)),
“application/vnd.android.package-archive”
);
-
startActivity(intent);
filePath是该文件的绝对路径。
uri要换为你要打开的文件绝对路径,类型是”
Android
.net.Uri“ 参考博主:
http://blog.csdn.net/weich_java/article/details/7002829
1.打开所有文件类型
-
Intent intent =
new
Intent();
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setAction(android.content.Intent.ACTION_VIEW);
-
intent.setDataAndType(uri,
“*/*”
);
2.打开apk文件
-
Intent intent =
new
Intent();
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setAction(android.content.Intent.ACTION_VIEW);
-
intent.setDataAndType(uri,
“application/vnd.android.package-archive”
);
3.打开Video文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
intent.setDataAndType(uri,
“video/*”
);
4.打开audio文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
intent.setDataAndType(uri,
“audio/*”
);
5.打开HTML文件
-
Uri uri2 = Uri.parse(uri).buildUpon().encodedAuthority(
“com.android.htmlfileprovider”
).scheme(
“content”
).encodedPath(param ).build();
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.setDataAndType(uri2,
“text/html”
);
6.打开Image文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(uri,
“image/*”
);
7.打开PPT文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(uri,
“application/vnd.ms-powerpoint”
);
8.打开Excel文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(uri,
“application/vnd.ms-excel”
);
9.打开word文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri,
“application/msword”
);
10.打开CHM文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri,
“application/x-chm”
);
11.打开文本文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(uri1,
“text/plain”
);
12.打开PDF文件
-
Intent intent =
new
Intent(
“android.intent.action.VIEW”
);
-
intent.addCategory(
“android.intent.category.DEFAULT”
);
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
intent.setDataAndType(uri,
“application/pdf”
);