通过模拟器和真机实验,以下代码已成功适配Andorid 10下载,适配方案在于修改文件路径
// 下载文件的路径
var path: String? = null
// 如果是Android 10以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
LogUtils.e("Android 10以上设备")
path = externalCacheDir?.path
}
OkGo.get<File>("http://60.179.32.61:2811/e/file/download?code=50325988f0466427&id=91313")
.execute(object : FileCallback(path, null) {
override fun onSuccess(response: Response<File>?) {
LogUtils.e("onSuccess")
val file = response?.body()
LogUtils.e("安装APK")
AppUtils.installApp(file)
}
override fun onError(response: Response<File>?) {
LogUtils.e("onError--->${response?.message()}")
}
override fun downloadProgress(progress: Progress?) {
LogUtils.e("${progress?.fraction}")
}
})