Last active
February 5, 2024 21:36
-
-
Save TWiStErRob/0b0b083225f2869b5cb1 to your computer and use it in GitHub Desktop.
Glide 3.6.0 proof of concept for loading a Bitmap as Bitmap through Glide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BitmapBitmapResourceDecoder implements ResourceDecoder<Bitmap, Bitmap> { | |
private final BitmapPool pool; | |
public BitmapBitmapResourceDecoder(Context context) { | |
this(Glide.get(context).getBitmapPool()); | |
} | |
public BitmapBitmapResourceDecoder(BitmapPool pool) { | |
this.pool = pool; | |
} | |
@Override public Resource<Bitmap> decode(Bitmap source, int width, int height) throws IOException { | |
return BitmapResource.obtain(source, pool); | |
} | |
@Override public String getId() { return "BitmapBitmapResourceDecoder"; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GenericRequestBuilder<Bitmap, Bitmap, Bitmap, Bitmap> glide = Glide | |
.with(context) | |
.using(new PassthroughModelLoader<Bitmap, Bitmap>(), Bitmap.class) | |
.from(Bitmap.class) | |
.as(Bitmap.class) | |
.decoder(new BitmapBitmapResourceDecoder(context)) | |
.cacheDecoder(new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(context))) | |
.encoder(new BitmapEncoder()) | |
// or .diskCacheStrategy(DiskCacheStrategy.NONE) instead of last 2 | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simulate a bitmap input coming from somewhere | |
Bitmap bitmap = getBitmap(android.R.drawable.sym_def_app_icon); | |
glide | |
.clone() // if you use the same glide multiple times (e.g. in Adapter) | |
.load(bitmap) | |
.signature(new StringSignature("android.R.drawable.sym_def_app_icon")) // required for cache to be correct | |
// ... add whatever you want here (transformation, animation ...) | |
.into(imageView) | |
; | |
Bitmap getBitmap(@DrawableRes int id) { | |
Drawable drawable = getResources().getDrawable(id); | |
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
drawable.draw(canvas); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PassthroughModelLoader<Result, Source extends Result> implements ModelLoader<Source, Result> { | |
@Override public DataFetcher<Result> getResourceFetcher(final Source model, int width, int height) { | |
return new CastingDataFetcher<Source, Result>(model); // upcasting is ok | |
} | |
/** Extremely unsafe, use with care. */ | |
private static class CastingDataFetcher<Source, Result> implements DataFetcher<Result> { | |
private final Source model; | |
public CastingDataFetcher(Source model) { | |
this.model = model; | |
} | |
@SuppressWarnings("unchecked") | |
@Override public Result loadData(Priority priority) throws Exception { | |
return (Result) model; | |
} | |
@Override public void cleanup() { } | |
@Override public String getId() { return ""; } | |
@Override public void cancel() { } | |
} | |
} |
I'm using your code to set 3 different bitmaps (from camera) to 3 different imageViews, after the first time, every time I open my activity, it loads the first 3 bitmaps I got from my camera no matter what. What do you think is the issue?
nvm, I converted the Bitmap to a byte array and used Glide.
@hatpick have you applied different signatures to the images? I suggest timestamp.
Also consider diskCacheStrategy(NONE) to prevent cache hit.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See bumptech/glide#122