android调用另一app的xml,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.

主要步骤:

定义一个FileProvider

指定可用文件

检索文件的内容URI

授予URI的临时权限

将内容URI提供给其他应用程序

定义一个FileProvider

首先需要在AndroidManifest.xml里申请关于文件的权限

或者

同样,在AndroidManifest.xml中添加以下代码,允许授权临时访问文件

...

...

android:name="android.support.v4.content.FileProvider"

android:authorities="com.mydomain.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

...

...

指定可用文件

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

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

示例如下

创建后需要在fileprovider中加入该xml文件

android:name="android.support.v4.content.FileProvider"

android:authorities="com.example.yyyyz.systonpad.fileprovider"

android:grantUriPermissions="true"

android:exported="false">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/filepaths" />

​ 在 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种不同的路径以及对应的函数

​ 等价于使用 Context.getFilesDir()

​ 等价于使用 getCacheDir()

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

等价于使用 `Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)`.

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值