OkGo下载文件适配Android 10

通过模拟器和真机实验,以下代码已成功适配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("http://60.179.32.61:2811/e/file/download?code=50325988f0466427&id=91313")
                .execute(object : FileCallback(path, null) {
                    override fun onSuccess(response: Response?) {
                        LogUtils.e("onSuccess")
                        val file = response?.body()
                        LogUtils.e("安装APK")
                        AppUtils.installApp(file) 
                    }

                    override fun onError(response: Response?) {
                        LogUtils.e("onError--->${response?.message()}")
                    }

                    override fun downloadProgress(progress: Progress?) {
                        LogUtils.e("${progress?.fraction}")
                    }
                })

你可能感兴趣的:(OkGo下载文件适配Android 10)