我正在尝试从Web服务器下载apk文件并将其存储在下载文件夹中。我正在使用feul库
Fuel.download("https://mypath/app.apk").destination {
response, url -> val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()) File(dir, "abcd.apk")
}.progress {
readBytes, totalBytes -> val progress = readBytes.toFloat() / totalBytes.toFloat()
Log.d("log",progress.toString())
}.response {
req, res, result -> Log.d("log","download completed ")
Log.d("log",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/abcd.apk")
}
我在bytesArray
中收到response
,但该文件未保存到下载文件夹。我的write_external_storage
文件上有Manifest
。请告诉我我错过了什么?
答案 0 :(得分:1)
正如您在评论中提到的,您使用targetSdkVersion
作为23.从Android 6.0开始(API级别23),如果您在应用中将targetSdkVersion
设置为23,则必须实施朗姆酒权限你的应用。
要实现它,请参阅此处的官方文档Self Join
答案 1 :(得分:0)
fun storeImage(link: String) : String {
val policy: StrictMode.ThreadPolicy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val cw = ContextWrapper(applicationContext)
val directory: File = cw.getDir("imageDir", Context.MODE_PRIVATE)
val pictureFile = File(directory, "backgroundImage.gif")
URL(link).openStream().use { input ->
FileOutputStream(pictureFile).use { output ->
input.copyTo(output)
Log.e("DD GIF", "storeImage: FILE SAVED" )
return "SAVED"
}
}
}
它对我有用。