intent.setDataAndType(uri, “application/x-chm”);
return intent;
}
// android获取一个用于打开文本文件的intent
public static Intent getTextFileIntent(String param, boolean paramBoolean) {
Intent intent = new Intent(“android.intent.action.VIEW”);
intent.addCategory(“android.intent.category.DEFAULT”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (paramBoolean) {
Uri uri1 = Uri.parse(param);
intent.setDataAndType(uri1, “text/plain”);
} else {
Uri uri2 = Uri.fromFile(new File(param)
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
);
intent.setDataAndType(uri2, “text/plain”);
}
return intent;
}
// android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(String param) {
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(new File(param));
intent.setDataAndType(uri, “application/pdf”);
return intent;
}
// android获取一个用于打开图片文件的intent
public static Intent getPicturefFileIntent(String param) {
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(new File(param));
intent.setDataAndType(uri, “image/*”);
return intent;
}
// android获取一个用于打开压缩包的intent (手机需安装能打开压缩文件的相关软件)
public static Intent getZipRarFileIntent(String param) {
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(new File(param));
intent.setDataAndType(uri, “application/x-gzip”);
return intent;
}
}
上面这个是核心的工具类,就是打开各种文档的跳转写法,然后在对应的点击事件调用 就可以,举列
protected void openFile(String path) {
String format = path.substring(path.lastIndexOf(".") + 1);
File file = new File(path);
try {
if (file.exists() || FileUtil.fileAvailable(file)) {
if (TextUtils.equals(“doc”, format) || TextUtils.equals(“docx”, format)) {
mContext.startActivity(IntentDocumentView.getWordFileIntent(path));
} else if (TextUtils.equals(“xls”, format) || TextUtils.equals(“xlsx”, format)) {
mContext.startActivity(IntentDocumentView.getExcelFileIntent(path));
} else if (TextUtils.equals(“zip”, format) || TextUtils.equals(“rar”, format)) {
mContext.startActivity(IntentDocumentView.getZipRarFileIntent(path));
}else if (TextUtils.equals(“pdf”, format) || TextUtils.equals(“PDF”, format)) {
mContext.startActivity(IntentDocumentView.getPdfFileIntent(path));
}else {
ToastUtil.showToast(mContext, “新增文件类型,请联系软件开发商”);
}
} else {
ToastUtil.showToast(mContext, “请先下载文件”);
}
} catch (Exception e) {
e.printStackTrace();
ToastUtil.showToast(mContext, “请先安装可以查看” + format + “格式的软件”);
}
}