我正在尝试使用 Glide 下载 jpg 文件。文件由承载者保护。我尝试了以下方法。没有承载保护的免费图像按预期工作。
suspend fun downloadUrlToBitmap(accessToken: String, bitmap: (Bitmap) -> Unit) {
val glideUrl = GlideUrl(
imageUrl,
LazyHeaders.Builder()
.addHeader("Authorization", "Bearer ".plus(accessToken))
.build()
)
Glide.with(context)
.asBitmap()
.load(glideUrl)
.transform(CenterCrop(), RoundedCorners(4))
.placeholder(R.drawable.ic_placeholder_photo)
.error(R.drawable.ic_placeholder_photo)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bitmap.invoke(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
Timber.d("onLoadCleared")
}
})
}
在Logcat中我可以看到以下错误
Load failed for https://myPictureUrl.jpeg with size [-2147483648x-2147483648]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There were 4 causes:
java.io.IOException(java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000)
java.io.IOException(java.lang.RuntimeException: setDataSource failed: status = 0x80000000)
java.io.IOException(java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000)
java.io.IOException(java.lang.RuntimeException: setDataSource failed: status = 0x80000000)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 6): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{DirectByteBuffer->Bitmap->Bitmap}, DATA_DISK_CACHE, https://myPictureUrl.jpeg
There was 1 cause:
java.io.IOException(java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{DirectByteBuffer->Bitmap->Bitmap}
There was 1 cause:
java.io.IOException(java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class java.io.IOException: java.lang.RuntimeException: setDataSourceCallback failed: status = 0x80000000
Cause (2 of 6): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Bitmap->Bitmap}, DATA_DISK_CACHE, https://myPictureUrl.jpeg
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->Bitmap}
Cause (3 of 6): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Bitmap->Bitmap}, DATA_DISK_CACHE, https://myPictureUrl.jpeg
There was 1 cause:
java.io.IOException(java.lang.RuntimeException: setDataSource failed: status = 0x80000000)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->Bitmap->Bitmap}
提前致谢
我终于成功了。问题是图像在服务器上进行了 Base64 编码,因此我必须在我这边对其进行解码。此外,我将下载从 Glide 切换到了 Retrofit。我所做的是以下
登录以获取访问令牌
然后像这样下载图像
@获取 fun downloadImage(@Header("Authorization") auth: String, @Url url: String): Call
然后获取响应并将其解码以制作位图
val demodImage = Base64.decode(response.body()!!.bytes(), Base64.DEFAULT) val 位图 = BitmapFactory.decodeStream(decodedImage.inputStream())