最近在使用蓝牙进行文件分享时,出现了一个奇怪的问题。同样的代码在android5.1上可以顺利运行,但是在android7.0上就运行失败。出现如下的错误:

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/bluetooth/data.txt exposed beyond app through ClipData.Item.getUri()

出现这个问题的时候我立刻意识到这是一个兼容的问题,于是在网上找了一些方法,并解决了这个问题,我受到启发的网址是:

​ 出现FileUriExposedException这样的异常,原因是Andorid7.0的“私有目录被限制访问”,“StrictMode API 政策”。
由于从Android7.0开始,直接使用真实的路径的Uri会被认为是不安全的,会抛出一个FileUriExposedException这样的异常。需要使用FileProvider,选择性地将封装过的Uri共享到外部。

即以前的共享代码是这样写的:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.setComponent(new ComponentName("com.android.bluetooth","com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));
startActivity(sharingIntent);

在android7.0版本以上时,不使用Uri.fromFile(),使用以下的代码:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.setComponent(new ComponentName("com.android.bluetooth","com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(BluetoothChat.this,"你的包名" + ".fileprovider", new File(path)));
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(sharingIntent);

​ 如果是Andorid7.0或以上,则不再使用Uri.fromFile()方法获取文件的Uri,而是通过使用FileProvider(support.v4提供的类)的getUriForFile()。同时要添加多这么一行代码

sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

由于FileProvider是继承ContentProvider,属于四大组件之一,需要在AndroidManifest.xml中配置,配置如下:

    <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="你的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<!--元数据-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>

其中android:resource="@xml/file_provider_paths"的内容你可以在res目录下新建一个xml文件夹,在文件夹中新建一个file_provider_paths.xml文件即可,如下图所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="" name="myFile"></external-path>
</paths>
</resources>

  上述代码中path=” “,是有特殊意义的,它代码根目录,也就是说你可以向其它的应用共享根目录及其子目录下任何一个文件了,如果你将path设为path=”pictures”, 那么它代表着根目录下的pictures目录(eg:/storage/emulated/0/pictures),如果你向其它应用分享pictures目录范围之外的文件是不行的。

分享文件代码如下:

    // 調用系統方法分享文件
public static void shareFile(Context context, File file) {
try{
if (null != file && file.exists()) {
Log.w("ppp-params", file.getPath());
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context,"app.zhaohangwuliu.com.appada" + ".fileprovider", file));
share.setType("*/*");//此处可发送多种文件
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "分享文件"));
} else {
//Toast.makeText(context, "分享文件不存在", Toast.LENGTH_SHORT).show();
}
}catch (Exception ex){
Log.w("ppp-params", ex);
} }

转: https://blog.csdn.net/kabuto_hui/article/details/78907572

android文件分享在android7.0以上版本报错的解决方案的更多相关文章

  1. appium desktop v1.2.7在android7.0上运行报错“Error: Error executing adbExec”

    1.参考下面链接,https://stackoverflow.com/questions/42283921/unable-to-run-appium-tests-on-android-7-0:得知需要 ...

  2. ve2.0 v-for循环报错的解决方案

    <li v-for="(item,index) in mokeData" class="page" :key="index"> ...

  3. Appium适配Android7.0以上版本

    Appium适配Android7.0以上版本 测试机型: 华为荣耀V9 安卓版本: Android7.0 appium版本: 1.65 说明: 公司新采购了一批安卓机器,拿了其中一台华为荣耀V9跑之前 ...

  4. centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'

    centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法 3.7版本需要一个新的包libffi-de ...

  5. 【原创】大叔问题定位分享(20)hdfs文件create写入正常,append写入报错

    最近在hdfs写文件的时候发现一个问题,create写入正常,append写入报错,每次都能重现,代码示例如下: FileSystem fs = FileSystem.get(conf); Outpu ...

  6. 0.4 IDEA报错以及解决方式

    0.4 IDEA报错以及解决方式一.端口被占用 [WARNING] FAILED SelectChannelConnector@0.0.0.0:8080: java.net.BindException ...

  7. iOS-C文件添加到iOS项目中,运行报错

    iOS-C文件添加到iOS项目中,运行报错 问题: 往项目中添加一个空的c文件, 编译运行; 出现2,30个编译错误. 原因: 由于在项目中添加了Pch文件,在文件中所有代码还没有开始运行之前, pc ...

  8. 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

    spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...

  9. ubuntu 16.04 anaconda 4.2.0 安装tensorflow 报错

    ubuntu 16.04 anaconda 4.2.0 安装tensorflow 报错. 安装pyenv后,在pyenv环境内安装 anaconda,然后再安装tensorflow不再报错,比较奇怪, ...

随机推荐

  1. 关于ping github.com超时的解决办法

    今天在使用git的时候执行将本地分支推送到远程分支的push操作时(同时为远程库创建和本地分支同名的分支),遇到了超时的错误,经过查询全网各位大牛的操作这里给出有效解决方式 进入C:\Windows\ ...

  2. JavaScript项目总结一

    1.类选择其下,第一个 $('selector').first()==$('selector:first')==$('selector:eq(0)') 2.如果要选择非第一个 $('selector: ...

  3. Helm 安装部署Kubernetes的dashboard

    Kubernetes Dashboard 是 k8s集群的一个 WEB UI管理工具,代码托管在 github 上,地址:https://github.com/kubernetes/dashboard ...

  4. hdu1540-Tunnel Warfare-(线段树+二分)

    题意:有n个村庄排成一列,相邻的村庄可以通信,炸毁则不可以通信,进行m个操作.3种操作,1.炸毁某村庄:2.修复上一个被炸毁的村庄:3.查询某个村庄能通信的村庄数(自己算一个). 解题:求某个点左边扩 ...

  5. csp-s 考前刷题记录

    洛谷 P2615 神奇的幻方 洛谷 P2678 跳石头 洛谷 P1226 [模板]快速幂||取余运算 洛谷 P2661 信息传递 LOJ P10147 石子合并 LOJ P10148 能量项链 LOJ ...

  6. 在WinDbg中显示和搜索std::vector内容

    WinDbg从来都不擅长可视化.尽管Visual Studio一直都有autoexp.dat,而且最近还出现了本机调试器可视化工具,但WinDbg用户不得不满足于转储内存区域和搜索内存来识别模式.另一 ...

  7. 删除ubuntu旧内核的方法

    https://www.jianshu.com/p/75edb9a5fbab 磁盘满了 需要清理系统盘 1,先用uname -a 查看当前内核版本: uname -a Linux 10-9-37-13 ...

  8. luogu_2605: 基站选址

    洛谷2605:基站选址 题意描述: 有\(N\)个村庄在一条直线上,第\(i(i>1)\)个村庄的距离第\(1\)个村庄的距离为\(D_i\). 需要在这些村庄中建立不超过\(K\)个通讯站,在 ...

  9. [RN] React Native 使用 AsyncStorage 存储 缓存数据

    React Native 使用 AsyncStorage 存储 缓存数据 AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的 ...

  10. AcWing 95 费解的开关

    目录 前言 题目链接 思路 代码 前言 博客咕咕咕了好久了,是时候写一下了 题目链接 AcWing 95 费解的开关 思路 首先可以看出 1.每一个位置顶多只会操作一次.因为如果操作两次的话,相当于不 ...