Android7.0做了一些权限更改,为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问。此设置可防止私有文件的原数据泄漏,同事Android7.0如果传递 file:// URI 会触发 FileUriExposedException 异常。适配Android7.0 FileProvider的步骤如下:

AndroidManifest.xml清单文件的修改
<manifest>    ......    <application>                <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="${applicationId}.FileProvider"            android:exported="false"            android:grantUriPermissions="true">            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_provider_paths" />        provider>    application>manifest>

${applicationId}.FileProviderapplicationId 为gradle项目写法,可修改为自己的包名。

file_provider_paths.xml文件
<paths>        <root-path        name="root"        path="." />        <files-path        name="files"        path="." />        <cache-path        name="cache"        path="." />        <external-path        name="external"        path="." />    <external-files-path        name="external_files"        path="." />    <external-cache-path        name="external_cache"        path="." />    <external-path        name="external_storage_root"        path="." />    <external-path        name="external_storage_files"        path="." />    <external-path        name="external_storage_cache"        path="." />paths>
代码中调用
    /**     * 安装APK     *     * @param context     * @return     */    public static void installApk(Context context, String apkPath) {        if (context == null || TextUtils.isEmpty(apkPath)) return;        File file = new File(apkPath);        if (!file.exists() || !file.isFile() || file.length() <= 0) return;        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        //判读版本是否在7.0以上        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            //provider authorities            Uri apkUri = FileProvider.getUriForFile(context, getPackageInfo(context).packageName + ".FileProvider", file);            //Granting Temporary Permissions to a URI            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");        } else {            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");        }        context.startActivity(intent);    }

更多相关文章

  1. Android中VideoView播放当前工程中视频文件的方法
  2. XML 文件XMLPull
  3. 项目文件跟Google学习Android开发-工具篇-Android Studio入门
  4. Android 文件及文件夹删除
  5. Android 监听U盘插入和拔出并获取U盘文件路径
  6. android 文件保存
  7. Android 页面或文件或网络请求时的加载动画
  8. Android 文件管理器

随机推荐

  1. Android(安卓)内存数据库
  2. 改变进度栏的颜色 progress bar 的背景色
  3. Android项目开发一
  4. mac添加android的adb等工具到环境变量
  5. error XA5205: Cannot find `aapt.exe`.
  6. android 中文 api (71) ―― BluetoothServ
  7. 51. (android开发)线性布局、相对布局、
  8. Android札记
  9. Android换肤机制
  10. Android点击事件的四种写法