Android 7.0 FileProvider适配

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

AndroidManifest.xml清单文件的修改
<manifest>
    ......
    <application>
        <!-- Android7.0适配 -->
        <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项目写法,可修改成本身的包名。java

file_provider_paths.xml文件
<paths>
    <!-- 国内因为rom比较多,会产生各类路径,好比华为的/system/media/,以及外置sdcard -->
    <root-path  name="root" path="." />
    <!-- 表明的根目录Context.getFilesDir() -->
    <files-path  name="files" path="." />
    <!-- 表明的根目录getCacheDir() -->
    <cache-path  name="cache" path="." />
    <!-- 表明的根目录Environment.getExternalStorageDirectory() -->
    <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);
    }

本文分享 CSDN - 秦川小将。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。android

相关文章
相关标签/搜索