近期公司有个需求,PDF格式需要在应用内打开浏览,Word、Excel要求不高直接用第三方软件打开即可,找了两种解决办法。
第一, 全部在应用内打开,可接入腾讯的TBS(没有做尝试),可参考链接接入TBS文章
第二,PDF可使用AndroidPdfViewer在应用内下载打开即可,Word、Excel下载后直接跳转至第三方软件打开
我选择了第二种办法,以后有空再尝试第一种解决办法吧。
以上两种办法都需要先下载文件再打开浏览,下载的解决方案这里就不描述了,网上很多,我使用的是廖子尧的第三方框架OkDownload,廖子尧GitHub可以学习一下,个人感觉还是挺好用的。
这里主要想记录下打开方式以及要注意的点:
android 7.0 新增私有目录访问权限,Google 官方说明:为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏。
所以这里在访问下载的文件存储路径时,也需要动态判断系统版本添加文件读取权限,使用FileProvider获取路径,否则会触发 FileUriExposedException异常
下面列举了三种打开文件类型的写法:
private void openFileIntent(String path, String type) {
if (!StringUtils.isNullOrWhiteSpace(path)) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(getActivity(), getApplicationContext().getPackageName() + ".provider", new File(path));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//给目标文件临时授权,必需添加
} else {
uri = Uri.fromFile(new File(path));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
if (StringUtils.isEqual(type, "doc")) {
intent.setDataAndType(uri, "application/msword");
this.startActivity(intent);
this.finish();
} else if (StringUtils.isEqual(type, "xls")) {
intent.setDataAndType(uri, "application/vnd.ms-excel");
this.startActivity(intent);
this.finish();
} else if (StringUtils.isEqual(type, "ppt")) {
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
this.startActivity(intent);
this.finish();
}
} catch (Exception e) {
MessageUtils.showCusToast(getActivity(), "没有找到打开该文件的应用程序");
finish();
}
}
}
再记录下setDataAndType设置所对应的值:
private final String[][] MIME_MapTable = {
//{后缀名,MIME类型}
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"}
};