8

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?

Tomas
  • 85
  • 1
  • 8

2 Answers2

6

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);
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • 5
    But the thing is that I would need to make request somewhere else, and then pass it to Glide. Then no caching and etc. I am looking ways for Glide handle base64 response – Tomas Aug 17 '17 at 14:01
2

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
    .........
}
Martyns
  • 3,605
  • 22
  • 33