在服务器 returns base64 时使用 glide 加载图像
Loading image with glide while server returns base64
当我用这样的东西加载图像时:
String url = "https://example.com/user/123123/profile_pic"
Glide.with(context).load(url).into(imageView);
服务器响应是 base64,默认情况下 glide 不处理它
我目前的解决方案:
load image with retrofit -> pass image encoded to glide
在这种情况下,我会丢失滑动缓存。我想知道是否有办法使用 glide 发出该请求并处理 base64 响应?
您可以将 Base64 字符串转换为字节,然后将该字节加载到 glide 中。
byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);
// here imageBytes is base64String
Glide.with(context)
.load(imageByteArray)
.asBitmap()
.into(imageView);
为 Glide 使用自定义 ModelLoader
https://bumptech.github.io/glide/tut/custom-modelloader.html
这个问题的好手册
在 class Base64DataFetcher
->
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super ByteBuffer> callback) {
String base64Section = getBase64SectionOfModel();
// get this from Retrofit or another
.........
}
当我用这样的东西加载图像时:
String url = "https://example.com/user/123123/profile_pic"
Glide.with(context).load(url).into(imageView);
服务器响应是 base64,默认情况下 glide 不处理它
我目前的解决方案:
load image with retrofit -> pass image encoded to glide
在这种情况下,我会丢失滑动缓存。我想知道是否有办法使用 glide 发出该请求并处理 base64 响应?
您可以将 Base64 字符串转换为字节,然后将该字节加载到 glide 中。
byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);
// here imageBytes is base64String
Glide.with(context)
.load(imageByteArray)
.asBitmap()
.into(imageView);
为 Glide 使用自定义 ModelLoader
https://bumptech.github.io/glide/tut/custom-modelloader.html
这个问题的好手册
在 class Base64DataFetcher
->
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super ByteBuffer> callback) {
String base64Section = getBase64SectionOfModel();
// get this from Retrofit or another
.........
}