问题描述
使用 Glide
:
private suspend fun downloadImage(context: Context,imageUrl: String,fileName: String,folderName: String) {
withContext(dispatchers.IO) {
val gifBuffer = Glide.with(context).asgif().load(imageUrl).submit().get().buffer
val imageUri = getimageUri(getApplication(),fileName,folderName)
context.contentResolver.openOutputStream(imageUri).use {
val bytes = ByteArray(gifBuffer.capacity())
(gifBuffer.clear() as ByteBuffer).get(bytes) //both ".clear()" and "as ByteBuffer" are mandatory!!
it?.write(bytes)
}
}
}
使用 URL().openStream()
private suspend fun downloadImage(context: Context,folderName: String) {
withContext(dispatchers.IO) {
val imageUri = getimageUri(getApplication(),folderName)
URL(imageUrl).openStream().use { inputStream ->
context.contentResolver.openOutputStream(imageUri).use { outputStream ->
inputStream.copyTo(outputStream!!)
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)