下载图片的最佳方法:Glide vs URL().openStream()

问题描述

哪种方法更好,为什么?以下是下载 GIF 文件的示例。

使用 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 (将#修改为@)