zihadrizkyef zihadrizkyef - 28 days ago 28
Android Question

Glide : load url as bitmap not working

I want to load bitmap from server using Glide. If it's success then put it in my ImageView then add it to my ArrayList. If it's fail (broken url, no internet, etc) then set ImageView to my default image. But Glide's listener not working at all, also LogCat not show "onException" and "onResourceReady" at all. What's wrong with my code?

Glide.with(activity.getApplicationContext())
.load(MainActivity.URL_SERVER + "/asset/" + imageUrl.getString(i))
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
ivImage.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.noimage));
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
ivImage.setImageDrawable(resource);
imageLoadedBitmap.add(((GlideBitmapDrawable) resource).getBitmap());
return false;
}
});

Answer

As of now, nothing in that method chain is actually triggering the image loading operation. You could use .into(ivImage) at the end and just use listener for adding bitmap to array. Or you could use .preload() for more customisation. Also, use .error(R.drawable.noimage) for error image.

Storing bitmaps in an array looks like a bad idea though. If you're loading images in a list, put glide call in your adapter and let glide take care of the catching itself.

Comments