Android-N(7.0)应用文件限制访问

  • android 7.0 新增私有目录访问权限

Google 官方说明:

为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏

官方文档:

https://developer.android.google.cn/about/versions/nougat/android-7.0-changes.html#perm

  • 7.0 以上访问 file://uri 触发 FileUriExposedException(安全异常)

例如安装 apk 的方法

public static void installApp(Context context, File file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

7.0 以上会报错,如下:


异常
  • 解决方案 使用 FileProvider

在 res 目录 xml 文件夹下创建你的 provider path 文件

manifest_folder

provider 文件内容

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="随便起名不能为空" path=""/>
<grant-uri-permission android:path="string"
    android:pathPattern="string"
    android:pathPrefix="string" />
</paths>

在 AndroidManifest 的 application 节点声明 provider

<application>
    ...

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="你的应用程序包名.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/ls_provider_paths"
            />

    </provider>

</application>

最后安装时获取临时权限

public static void installApp(Context context, File file) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri;
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // 7.0 以上
        uri = FileProvider.getUriForFile(context, "你的应用程序包名.provider", file);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        // 7.0 以下
        uri = Uri.fromFile(file);
    }
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
}
  • 注意

如果你引用的第三方包已经使用了 provider 会报如下错误:

build_error

原因:声明冲突

解决办法:打开 AndroidManifest 文件 Tab 页中的 Merged Manifest 发现冲突 log ,并且系统已给出了解决建议:

声明冲突

原来是 provider 节点的 authorities 声明和 meta-data 节点的 resource 声明,将这俩覆盖

<application>
    ...

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="你的应用程序包名.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities"
        >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/ls_provider_paths"
            tools:replace="android:resource"
            />

    </provider>

</application>

到此,问题解决。

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,353评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,524评论 0 17
  • 面试题总结 通用 安卓学习途径, 寻找资料学习的博客网站 AndroidStudio使用, 插件使用 安卓和苹果的...
    JingBeibei阅读 1,715评论 2 21
  • 两数交换的四种方法: 三元运算符实现的都可以采用if语句实现,反之不成立 什么时候if语句实现不能用三元改进呢? ...
    baiyun520960阅读 158评论 0 0
  • 企业证书签名,签名过后无法安装,最初以为是证书问题。用其他ipa文件重签,安装正常。用ad-hoc重签,安装失败。...
    秦枫桀阅读 5,558评论 1 1