Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Glide 3.6.0 proof of concept for loading a Bitmap as Bitmap through Glide
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"; }
}
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
;
// 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);
}
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() { }
}
}
@TWiStErRob

This comment has been minimized.

Copy link
Owner Author

@TWiStErRob TWiStErRob commented May 6, 2015

@hatpick

This comment has been minimized.

Copy link

@hatpick hatpick commented Jul 8, 2015

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.

@TWiStErRob

This comment has been minimized.

Copy link
Owner Author

@TWiStErRob TWiStErRob commented Oct 19, 2015

@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