IM中常有的功能之一,发文件。发完自然需要打开。可是Android 并不能像IOS那样,可以用webview直接打开所有的文件类型。
but产品要求,要和ios一样,用webview打开文件,最终接入腾讯X5内核浏览器,webview打开文件。

webview方式打开文件

  • X5内核的接入,不过多说,看一下官方文档https://x5.tencent.com/tbs/
  • 下载对应的sdk Demo 将Jar包和so文件,放到项目中,application中初始化就完成集成

参考 钟离四郎 对x5的封装。https://github.com/ZhongXiaoHong/superFileView

先看效果:

我直接使用 四郎的SuperFileView2控件,他是封装了打开文件的初始化工作。
使用很简单

 <com.airtalk.view.SuperFileView2android:id="@+id/superFileView"android:layout_width="match_parent"android:layout_height="match_parent" />
 //展示的方法superFileView.setOnGetFilePathListener(new SuperFileView2.OnGetFilePathListener() {@Overridepublic void onGetFilePath(SuperFileView2 mSuperFileView2) {superFileView.displayFile(new File(finalPath));}});superFileView.show();
//  销毁时一定要停止展示,不然不会释放,无法第二次打开
@Overrideprotected void onDestroy() {super.onDestroy();if (null != superFileView) {superFileView.onStopDisplay();}}

至此,就可以实现webview方式,打开文件!

其他方式打开文件

  • android提供Intent意图方式,传入文件格式,自动调用合适的外部应用打开文件。
    需要注意的是,android7.0共享文件的权限发生改变,并做出解释:

对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。


并给出解决方案

要在应用间共享文件,您应发送一项 content:// URI,并授予 URI 临时访问权限。进行此授权的最简单方式是使用 FileProvider 类。如需了解有关权限和共享文件的详细信息,请参阅共享文件。
https://developer.android.com/about/versions/nougat/android-7.0-changes.html#accessibility


所以我们用Intent打开文件时,需要做版本判断,7.0以上需要使用FileProvider来获取file的uri来进行传递。

// Android获取一个用于打开APK文件的intentpublic static Intent getApkFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "application/vnd.android.package-archive");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开VIDEO文件的intentpublic static Intent getVideoFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.putExtra("oneshot", 0);intent.putExtra("configchange", 0);intent.setDataAndType(uri, "video/*");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开AUDIO文件的intentpublic static Intent getAudioFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.putExtra("oneshot", 0);intent.putExtra("configchange", 0);intent.setDataAndType(uri, "audio/*");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开Html文件的intentpublic static Intent getHtmlFileIntent(String param) {Uri uri = Uri.parse(param).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param).build();Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(uri, "text/html");return intent;}// Android获取一个用于打开图片文件的intentpublic static Intent getImageFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "image/*");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开PPT文件的intentpublic static Intent getPptFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "application/vnd.ms-powerpoint");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开Excel文件的intentpublic static Intent getExcelFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "application/vnd.ms-excel");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开doc,Word文件的intentpublic static Intent getWordFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "application/msword");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开CHM文件的intentpublic static Intent getChmFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "application/x-chm");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开文本文件的intentpublic static Intent getTextFileIntent(String param) {Intent intent = null;try {Uri uri;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));} else {uri = Uri.fromFile(new File(param));}intent = new Intent(Intent.ACTION_VIEW);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setDataAndType(uri, "text/plain");} catch (Exception e) {e.printStackTrace();}return intent;}// Android获取一个用于打开PDF文件的intentpublic 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 | Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);Uri uri = FileProvider.getUriForFile(UIUtils.getContext(), UIUtils.getContext().getApplicationContext().getPackageName() + ".provider", new File(param));intent.setDataAndType(uri, "application/pdf");return intent;}

Android打开文件相关推荐

  1. android打开文件及打开方式(打开程序列表)

    [java] view plain copy //打开文件时调用 public void openFiles(String filesPath) { Uri uri = Uri.parse(" ...

  2. android打开文件方法

    通过MIME type的不同用系统含有的程序打开 [java] view plaincopy Uri uri = Uri.parse("file://" + filePath); ...

  3. android本地xml文件怎么打开,android 打开本地文件

    首先要知道的是,Android 打开本地文件是根据类型打开的,也就是根据文件的 MIME 类型来确定 如果不知道是什么类型,那就是 : */* 类型匹配表: private static final ...

  4. 打开别人Xamarin项目找不到android.jar文件

    打开别人Xamarin项目找不到android.jar文件 错误信息:Could not find android.jar for API Level 23. 打开非本机创建的Xamarin项目,编译 ...

  5. Xamarin中打开别人项目找不到android.jar文件

    Xamarin中打开别人项目找不到android.jar文件 错误信息:Could not find android.jar for API Level 23. 打开非本机创建的Xamarin项目,编 ...

  6. 【Android 逆向】Android 进程注入工具开发 ( 远程进程 注入动态库 文件操作 | Android 进程读取文件所需的权限 | fopen 打开文件标志位 | 验证文件权限 )

    文章目录 前言 一.Android 进程读取文件所需的权限 二.fopen 打开文件标志位 三.验证文件权限 前言 一.Android 进程读取文件所需的权限 通过 注入工具 , 将 libbridg ...

  7. 【Android 内存优化】Android 工程中使用 libjpeg-turbo 压缩图片 ( 初始化压缩对象 | 打开文件 | 设置压缩参数 | 写入压缩图像数据 | 完成压缩 | 释放资源 )

    文章目录 一.使用 libjpeg-turbo 压缩图片流程 二.初始化 JPEG 压缩对象 三.打开文件 四.设置压缩参数 五.开始压缩 六.循环写入压缩数据 七.完成图片压缩及收尾 八.libjp ...

  8. 显示android缓存文件,android – 使用ACTION_VIEW在缓存目录中打开文件

    我一直在寻找这个,但我无法让它正常工作.让我解释. 我有一个Android应用程序,可以将文件(图像,文档,-)保存在缓存目录中.起初我曾经使用getExternalCacheDir()方法并将它们保 ...

  9. Android调用系统软件打开文件(包括apk文件)

    应用中如何调用系统所装的软件打开一个文件,这是我们经常碰到的问题 /** * 打开文件 * @param file */ private void openFile(File file){ Inten ...

最新文章

  1. linux 根目录爆满 解决 /dev/mapper/centos-root 100%问题
  2. 『Python × C++』函数传参机制学习以及对比
  3. 4.12任务 apache结合php以及apache默认虚拟主机
  4. java获取数组的最小值_Java 数组获取最大和最小值的实例实现
  5. mysql数据表内容_MySQL数据表
  6. UVALive 7455 Linear Ecosystem (高斯消元)
  7. 漫步数理统计四——概率集合函数(下)
  8. Leadership_领导力和团队管理
  9. File指定过滤器得到需要的文件
  10. 如何将jar包加入到Maven本地仓库
  11. 计算机网络与通信之计算机网络体系结构
  12. 线程的学习,和线程的相关概念及多线程的学习指引
  13. yaourt下载速度太慢_加快Yaourt软件包的安装速度
  14. 二阶边值问题的数值解matlab,二阶线性微分方程边值问题的MATLAB求解
  15. 【历史上的今天】10 月 3 日:网络空间独立宣言之父诞生;3D 打印概念面世;eBay 收购 PayPal
  16. 想在Android应用程序中将XLS文件转换为PDF吗?看完这篇教程就懂了
  17. ios7新特性--4
  18. Centos7 安装Chrome浏览器
  19. 论文MICO for MRI bias field estimation and tissue segmentation品讲
  20. 响应式图像--图片自适应大小

热门文章

  1. uniapp简单生成海报
  2. 扬帆志远—tiktok跨境电商运营流程
  3. 你知道你的电脑 1 秒钟能做多少事情吗?
  4. 刺激战场最强压枪口诀,另附刺激战场模拟器和平板灵敏度
  5. 乐高幻影忍者系列诞生十周年,推出复刻经典套装与时尚联名系列
  6. 硬盘对拷速度很慢是什么原因该如何解决?
  7. 如何编写一个足球点球大战的模拟程序(C语言)
  8. polly php实例,亚马逊Amazon Polly文本转换语音PHP简明调用示例
  9. C语言删除字符串中指定字符
  10. ubuntu 在使用U盘安装时出现的问题总结