Home > Software engineering >  How to load base64 images from database and show them in Glide?
How to load base64 images from database and show them in Glide?

Time:01-10

Could you please help me? I got a list of images from the database that come in base64 format. I need to show those images using Glide. How can I convert base64 format so that Glide could understand them? Thank you.

CodePudding user response:

To show an image in Glide that is in base64 format, you will first need to decode the base64 string and convert it into a byte array. You can then use the byte array to create a Bitmap object, which can be displayed in an ImageView using Glide.

// Load the base64 string from the database
String base64String = "base64ImageReponse";

// Decode the base64 string into a byte array
byte[] imageBytes = Base64.decode(base64String, Base64.DEFAULT);

// Convert the byte array into a Bitmap object
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

// Load the Bitmap object into an ImageView using Glide
ImageView imageView = findViewById(R.id.image_view);
Glide.with(this).load(bitmap).into(imageView);

Please remember that base64 strings can be quite long, so decoding and converting them into a Bitmap object can be resource-intensive. Therefore it's recommended to decode the base64 strings in a background thread to avoid slowing down the main UI thread.

  • Related