最近在使用蓝牙进行文件分享时,出现了一个奇怪的问题。同样的代码在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文件即可,如下图所示

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目录范围之外的文件是不行的。

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. 读取android手机流量信息
  6. android EditText设置不可写
  7. android 使用html5作布局文件: webview跟javascript交互
  8. android studio调试c/c++代码
  9. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces

随机推荐

  1. Android 透明状态栏实现方案
  2. one mail for difference between LOCAL_
  3. 写一个android小闹钟
  4. android之bitmap和byte[]互转
  5. AndFire防火墙1.2版本发布
  6. android获取设备唯一标识device_token
  7. Android Dialog 去除背景内容模糊
  8. 自定义Android(cacerts.bks)添加根证书
  9. 解决Cocos2d-x3.1编译生成Android程序出
  10. Android(安卓)ANR原因分析(基于traces.tx