Android 7.0+调用其他App打开文件

​ 近期需要用到读取Android设备外存中的JSON和Word,在前期开发时用于调试的是Android 6.0的米4,当拿到实际应用环境(Android 7.0)时,Android 6.0之后的版本增加了运行时权限,应用程序在执行每个需要系统权限的功能时,需要添加权限请求代码(默认权限禁止),否则应用程序无法响应;查阅官方文档后发现可以使用FileProvider解决该问题

FileProvider概述

官方描述:FileProvider是ContentProvider的一个特殊的子类,通过创建content:// Uri来替代file:///Uri分享文件给另一个App,来促进安全共享。

原文: FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.

主要步骤:

  1. 定义一个FileProvider
  2. 指定可用文件
  3. 检索文件的内容URI
  4. 授予URI的临时权限
  5. 将内容URI提供给其他应用程序

官方文档链接:(https://developer.android.com/reference/android/support/v4/content/FileProvider.html#ServeUri)

定义一个FileProvider

  1. 首先需要在AndroidManifest.xml里申请关于文件的权限
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

或者

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  1. 同样,在AndroidManifest.xml中添加以下代码,允许授权临时访问文件

    <manifest>
        ...
        <application>
            ...
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.mydomain.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                ...
            </provider>
            ...
        </application>
    </manifest>  
    

指定可用文件

  1. res 下新建一个xml文件夹,并新建一个 filepath.xml 文件,使用 paths 指定具体文件路径,

pathsname 属性是用于之后的调用,path 属性对应我们需要的文件目录在对应父节点的路径

示例如下

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path  name="external_storage" path="docs/"/>
</paths>
  1. 创建后需要在fileprovider中加入该xml文件
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.yyyyz.systonpad.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

​ 在 provider 中加入一个 meta-data 元素 , android:name 属性设为以上默认值即可, android:resource 设为刚才所添加的xml文件文件名。

为文件生成Content Uri

在完成以上两步后即可在程序中引用你所需要的文件,示例如下

  File docPath = new File(mContext.getExternalCacheDir(), "docs");
  File file = new File(docPath, cadre.getXm() + ".doc");
  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
      Uri uri = getUriForFile(mContext,
                              BuildConfig.APPLICATION_ID+ ".fileprovider",
                              file);
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      // 设置文件类型为Word
      intent.setDataAndType(uri, "application/msword");
  } else{
      Uri uri = Uri.fromFile(file);
      intent.setDataAndType(uri, "application/msword");
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

​ 这里 mContext.getExternalCacheDir() 中获取的路径 Android\data\包名\cache 我需要的文件路径为Android\data\包名\cache\docs\ 所以在 child 中设为 “docs“ 。

​ 之后对Android系统版本进行判断,若>=7.0,则创建 content://Uri , 否则创建 file:///Uri

getUriForFile() 的三个参数 第一个context 无需多说、第二个参数为 包名 + .fileprovider , 第三个参数为之前创建的要打开的file。

为Uri生成暂时的Permissions

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

将Uri提供给其他程序

intent.setDataAndType(uri, "application/msword");
 mContext.startActivity(intent);

官方文档中介绍了以下6种不同的路径以及对应的函数

<files-path name="name" path="path" />

​ 等价于使用 Context.getFilesDir()

<cache-path name="name" path="path" />

​ 等价于使用 getCacheDir()

<external-path name="name" path="path" />

​ 等价于使用 Environment.getExternalStorageDirectory().

<external-files-path name="name" path="path" />
等价于使用   `Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)`.
<external-cache-path name="name" path="path" />

​ 等价于使用 Context.getExternalCacheDir().

<external-media-path name="name" path="path" />

​ 等价于使用 Context.getExternalMediaDirs(). 需要在api21+中使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,817评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,783评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 在书中的最后写道“他上了二级平台,沿着铁路线急速地向东走去。他远远地看见,头上包着红纱巾的惠英,胸前飘着红领巾的明...
    松果阅读 689评论 0 3
  • 苏州,古称吴,简称为苏,又称姑苏、平江等。自古就被文人称作人间天堂,也有云:上有天堂,下有苏杭 的美誉...
    一马当先乎阅读 794评论 11 9