Andrid自动更新安装APK时报错 android.os.FileUriExposedException

323 阅读1分钟

查询之后大部分都是以下解决方法:

1、AndroidManifest.xml写入

<application>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="完整包名.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>
</application>

2、创建res/xml/file_paths

文件中写入:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path path="Android/data/填写你自己的报名/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

完成以上步骤,你可能还会报错

一直报错,怎么运行都不行

解决方法:

步骤1、 自定义一个类继承 FileProvider,不用重写任何方法



import androidx.core.content.FileProvider;

public class MyFileProvider extends FileProvider {

}

步骤2、修改AndroidManifest.xml

<provider
            android:name=".MyFileProvider" //修改自己重写的MyFileProvider类即可
            android:authorities="完整包名.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
       
 </provider>