这个原因很简单,这个错误是检查AndroidManifest.xml,里面的
1:在AndroidManifest.xml配置以下配置
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="包名.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
![](https://img.haomeiwen.com/i1722765/27d5b66205bc9a13.png)
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-files-path name="images" path="Pictures" />
<files-path path="files" name="files" />
<cache-path path="files" name="cache" />
<external-path path="files" name="external" />
<external-files-path path="files" name="externalfiles"/>
<!-- 此标签需要 support 25.0.0以上才可以使用-->
<external-cache-path path="files" name="externalcache"/>
<external-path name="camera_photos" path="." />
<external-cache-path path="my_images" name="Android/data/com.gs.common/files/Pictures/"/>
<external-cache-path path="images" name="Pictures/"/>
<external-cache-path path="dcim" name="DCIM/"/>
<files-path name="images" path="Android/data/com.example.package.name/files/Pictures/OriPicture/" />
<external-path name="images" path="Android/data/com.example.package.name/files/Pictures/OriPicture/" />
<external-files-path name="images" path="files/Pictures/OriPicture" />
<root-path name="images" path="" />
</paths>
</resources>
尤其注意android:authorities="包名.fileprovider",注意大小写
app安装
//安装apk
fun installApk(context: Context, file: File) {
if (context == null) return
val authority: String =
getApplicationContext().packageName.toString() + ".fileProvider"
val apkUri: Uri = FileProvider.getUriForFile(context, authority, file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//判读版本是否在7.0以上
if (Build.VERSION.SDK_INT >= 24) {
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)
//弹出安装窗口把原程序关闭。避免安装完毕点击打开时没反应
android.os.Process.killProcess(android.os.Process.myPid())
}
网友评论