Android 常见问题七:安装/存放文件问题

文件地址

从Android 7.0开始,不再允许在app中把file:// Uri暴露给其他app,否则应用会抛出FileUriExposedException。原因在于,Google认为使用file:// Uri存在一定的风险。比如,文件是私有的,其他app无法访问该文件,或者其他app没有申请READ_EXTERNAL_STORAGE运行时权限。解决方案是,使用FileProvider生成content:// Uri来替代file:// Uri。

解决步骤

1.声明FileProvider 首先在清单文件中申明FileProvider。

<manifest>  
    ...  
    <application>  
        ...  
    <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        ...  
    </application>  
</manifest>  
  • android:name是固定写法。
  • android:authorities可自定义,是用来标识该provider的唯一标识,建议结合包名来保证authority的唯一性。
  • android:exported必须设置成 false,否则运行时会报错java.lang.SecurityException: Provider must not be exported 。
  • android:grantUriPermissions用来控制共享文件的访问权限。

2.添加file_paths.xml文件 文件格式如下

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<external-path path="Android/data/zhwl.com.simplsale/" name="files_root" />-->
    <!--<external-path path="." name="external_storage_root" />-->

    <external-files-path name="update_download" path="download" />
    <external-path name="external_files" path="."/>
    <cache-path
        name="picture_files"
        path="."/>
</paths>


其中根元素<paths>是固定的,内部元素可以是以下节点:
- <files-path name="name" path="path" /> 对应getFilesDir()。
- <cache-path name="name" path="path" /> 对应getCacheDir()。
- <external-path name="name" path="path" /> 对应Environment.getExternalStorageDirectory()。
- <external-files-path name="name" path="path" /> 对应getExternalFilesDir()。
- <external-cache-path name="name" path="path" /> 对应getExternalCacheDir()。

3.在Java代码中使用FileProvider

 /**
     * 安装apk
     *
     * @param
     */
    private void installApk(File apkFile) {
        if (!apkFile.exists()) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        mContext.startActivity(intent);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值