打开后缀.apk的文件,即启动安装程序;
//apkFilePath 文件路径
public void installAPK(String apkFilePath) {
// 创建URI
Uri uri = Uri.fromFile(new File(apkFilePath));
Intent intent = new Intent(Intent.ACTION_VIEW);
// 设置Uri和类型
intent.setDataAndType(uri, "application/vnd.android.package-archive");
// 执行安装
mContext.startActivity(intent);
} /**
* 打开多种类型文件
* @param path 文件路径
* @param type 文件类型
*/
public void openText(String path , int type){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(path ));
//判断文件类型
if (FILE_TYPE_PPT == type) {
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if (FILE_TYPE_WORD == type) {
intent.setDataAndType(uri, "application/msword");
} else if(FILE_TYPE_EXCEL == type){
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if(FILE_TYPE_TXT == type){
intent.setDataAndType(uri, "text/plain");
} else if(FILE_TYPE_PDF == type){
intent.setDataAndType(uri, "application/pdf");
} else if(FILE_TYPE_HTML == type){
Uri htmluri = Uri.parse(path).buildUpon().encodedAuthority("com.android.htmlfileprovider")
.scheme("content").encodedPath(path).build();
intent.setDataAndType(htmluri, "text/html");
}
try {
activity.startActivity(intent);
} catch (Exception e) {
Toast.makeText(mContext, "设备中没有安装支持该格式的程序", Toast.LENGTH_SHORT).show();
}
}<pre name="code" class="java"> ////////打开多媒体类型 intent.setDataAndType(uri, "audio/*"); //音频 intent.setDataAndType(uri, "video/*"); //视频 intent.setDataAndType(uri, "image/*"); //图片 intent.setDataAndType(uri, "application/x-chm"); //打开chm文件
////判断文件名是否是某种类型的后缀
private boolean check(final String name, final String[] extensions) {
for (String end : extensions) {
if (name.toLowerCase().endsWith(end)) {
return true;
}
}
return false;
}
/////////////设置类型
if (check(name, ".apk")){
file.setType(FILE_TYPE_APK);
} else if(check(name, ".pdf")){
file.setType(FILE_TYPE_PDF);
} else if(check(name,
getStringArray(R.array.ppt_filter))){
file.setType(FILE_TYPE_PPT);
}
...................
array.ppt_filter:
<array name="ppt_filter">
<item>.ppt</item>
<item>.pptx</item>
</array>
//获取设备存储路径(sd卡,usb)
Environment.getExternalStorageDirectory();
Environment.getExternalStorageDirectory().getParent();
笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等),布布扣,bubuko.com
笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
原文地址:http://blog.csdn.net/only_tan/article/details/32081735