Tomas
Tomas

Reputation: 85

Loading image with glide while server returns base64

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?

Upvotes: 8

Views: 4928

Answers (2)

Ivan Pursheha
Ivan Pursheha

Reputation: 36

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
    .........
}

Upvotes: 2

Chirag
Chirag

Reputation: 56925

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);

Upvotes: 6

Related Questions