Reputation: 995
Here is my code to display already downloaded base64 strings into an imageview (present in a ListView):
RequestOptions requestOptions = new RequestOptions();
requestOptions.override((int) Converter.pxFromDp(this.context, (float) ICON_SIZE),(int) Converter.pxFromDp(this.context, (float) ICON_SIZE));
requestOptions.dontAnimate();
requestOptions.fitCenter();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
if (!eventItem.getVulnerabilityIcon().isEmpty() ) {
Glide.with(context)
.asBitmap()
.load(Base64.decode(base64String, Base64.DEFAULT))
.apply(requestOptions)
.into(new BitmapImageViewTarget(holder.iconVul) { })
;
} else {
Glide.with(context).clear(holder.iconVul); // tell Glide that it should forget this view
holder.iconVul.setImageResource(R.drawable.defaultIcon); // manually set "unknown" icon
}
When I scroll the listview, all is fine (up and down). Now when I click on an item and I have a highlight(View view,int position) method that just changes the background of the view and thus calls getView(...). It then redraws all the views (normal behavior). The problem is that it redraws the image from scratch as if there was no cache feature applied on the image at all.The effect is that each time I click/tap on an item of the list, the image blinks, I don't want that effect.
At first I thought it was an animation problem and I found that, to disable any animation, I should've added dontAnimate(). Then, I went through glide's ( com.github.bumptech.glide:glide:4.0.0-RC0 ) documentation and read this:
/**
* From: RequestBuilder Class in com.github.bumptech.glide:glide:4.0.0-RC0
* Returns a request to load the given byte array.
*
* <p> Note - by default loads for bytes are not cached in either the memory or the disk cache.
* </p>
*
* @param model the data to load.
* @see #load(Object)
*/
public RequestBuilder<TranscodeType> load(@Nullable byte[] model) {
return loadGeneric(model).apply(signatureOf(new ObjectKey(UUID.randomUUID().toString()))
.diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true /*skipMemoryCache*/));
}
Now my question is this, is there a way to force Glide to cache byte array image after loading it the first time?
P.S: English is not my first tongue so please apologies for my poor grammar.
Upvotes: 2
Views: 3374
Reputation: 995
Changing the .into() part of the Glide call worked:
RequestOptions requestOptions = new RequestOptions();
requestOptions.override(iconWidth,iconHeight);
requestOptions.dontAnimate();
requestOptions.fitCenter();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
if (!eventItem.getVulnerabilityIcon().isEmpty() ) {
Glide.with(context)
.asBitmap()
.load(Base64.decode(base64String, Base64.DEFAULT))
.apply(requestOptions)
.into(new SimpleTarget<Bitmap>(iconWidth, iconHeight) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
finalHolder.iconVul.setImageBitmap(resource);
}
});
} else {
Glide.with(context).clear(holder.iconVul); // tell Glide that it should forget this view
holder.iconVul.setImageResource(R.drawable.defaultIcon); // manually set "unknown" icon
}
Now it doesn't blink anymore. Can one explain to me what is the difference? 1- Is it that because I use SimpleTarget that Glide finally caches the result? 2- Is it because the transition variable is not further processed?
Please can someone illuminates my lantern.
Upvotes: 2