Skip to content Skip to sidebar Skip to footer

How To Load Image Through Byte Array Using Glide?

I have an image contents byte[] form. But when i load them through Glide then broken images are shown. what I'm doing is shown below. Glide.with(context) .load(imageByteArray)

Solution 1:

Lets say your base64 string is

StringimageBytes="HVao14fpmtHSev3OgsrQNsawkFzXNcY3BsfQla6..."

You should convert imageBytes String to array of bytes through

byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);

afterwards pass this imageByteArray to Glide.

Glide.with(context)
    .load(imageByteArray)
    .asBitmap()
    .placeholder(R.drawable.ic_broken)
    .into(rowImageView);

Solution 2:

You can convert Base64 String to image using the following

Glide.with(context)
    .load(Base64.decode(base64ImageString, Base64.DEFAULT))
    .asBitmap()
    .placeholder(R.drawable.ic_broken)
    .into(rowImageView);

Solution 3:

If you are getting error on .asBitmap() just use it like this

             Glide.with(this)
                .asBitmap()
                .load(imageAsBytes)
                .into(image)

Solution 4:

Kotlin way:

val imageByteArray = Base64.decode(sampleBase64ImageString, Base64.DEFAULT)
Glide.with(this).load(imageByteArray).into(imageView)

Solution 5:

I do it a bit differently

byte[] bytes = Glide.with(context)
                        .as(byte[].class)
                        .load(url)
                        .submit()
                        .get();

The bytes are ready for file writing.

Post a Comment for "How To Load Image Through Byte Array Using Glide?"