When I load an image with something like this:
String url = "https://example.com/user/123123/profile_pic"
Glide.with(context).load(url).into(imageView);
server response is in base64 and glide doesn't handle it by default
My current solution:
load image with retrofit -> pass image encoded to glide
in this case I would lose glide caching. I wonder if there is a way to make that request with glide and handle base64 response?
imageByteArray successfully converts to bitmap without using glide.
Loading images with the unknown size If a loaded image happens to be smaller than the ImageView, Glide will unnecessarily expand original bitmap to match the size of the target view. One way to prevent it is to set scaleType=”centerInside” in the ImageView. That way loaded image won't expand beyond the original size.
How does the Glide caching mechanism work? By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.
Base64 images are primarily used to embed image data within other formats like HTML, CSS, or JSON. By including image data within an HTML document, the browser doesn't need to make an additional web request to fetch the file, since the image is already embedded in the HTML document.
You can convert Base64 String to byte then load that byte into glide.
byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);
// here imageBytes is base64String
Glide.with(context)
.load(imageByteArray)
.asBitmap()
.into(imageView);
Use custom ModelLoader for Glide
https://bumptech.github.io/glide/tut/custom-modelloader.html
good manual for this problem
In class Base64DataFetcher
->
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super ByteBuffer> callback) {
String base64Section = getBase64SectionOfModel();
// get this from Retrofit or another
.........
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With