androidauthorizationandroid-glidebearer-token

Glide Image Download with Bearer fails


I'm trying to download a jpg File with Glide. The File is secured with a Bearer. I tried the following. Free images without a Bearer protection work as expected.

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")
            }
        })
}

In Logcat I can see the following errors

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}

Thanks in advance


Solution

  • I finally got it working. The problem was that the image was base64 encoded on the server so I had to decode it on my side. Furthermore I switched for the download from Glide to retrofit. What I do is the following

    a) login to get an access token

    b) then download the image like this

    @GET
    fun downloadImage(@Header("Authorization") auth: String, @Url url: String): Call<ResponseBody>
    

    c) then take the response and decode it to to make a bitmap afterwards

    val decodedImage = Base64.decode(response.body()!!.bytes(), Base64.DEFAULT)
    val bitmap = BitmapFactory.decodeStream(decodedImage.inputStream())