10

I am using Glide to download and cache images on Android. Everything works well except the fact that I don't want to load the bitmaps directly into the ImageView, I don't want to have the fade animation, neither image placeholders.

All I want is to create a global method which will help me to download the image all over the application.

public class MyApp extends Application {

   public static void downloadImage(String url, final OnImageLoadedCallback callback) {

     // And how to implement the listener ?

     RequestListener<String, Bitmap> requestListener = new RequestListener<String, Bitmap() {
        @Override
        public boolean onException(Exception exc, String string, Target<Bitmap> target, boolean isFirstResource) {

           callback.onDone(null);               

           return false;
        }

        @Override
        public boolean onResourceReady(Bitmap bitmap, String string, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {

            callback.onDone(bitmap);

            return false;
        }
     };

      Glide.with(context)
           .load(url)
           .asBitmap()
           .dontAnimate()
           .diskCacheStrategy(DiskCacheStrategy.SOURCE)
           .listener(requestListener);
   }

}

The problem is that I don't know how to implement the listener. RequestListener isn't called at all.

Sam Judd
  • 7,017
  • 1
  • 33
  • 37
Zbarcea Christian
  • 8,320
  • 16
  • 74
  • 124

3 Answers3

15

Loads in Glide don't start until you make a call to into. The RequestListener interface observes requests, but isn't typically meant for handling results. Instead of using RequestListener, consider having your callback implement the Target interface and pass it in using into.

Alternatively you could just extend SimpleTarget and pass it in to each request in the same way you're trying to use RequestListener:

Target target = Glide.with(context)
     ...
     .into(new SimpleTarget<Bitmap>(width, height) {
          @Override
          public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
              callback.onDone(resource);
          }

          @Override
          public void onLoadFailed(Exception e, Drawable errorDrawable) {
              callback.onDone(null);
          }
     });

// At some point later, if you want to cancel the load:
Glide.clear(target);

You will want to provide a width and a height so that Glide can downsample and transform images appropriately. You may also run in to issues with cancellation if you're displaying these Bitmaps in views, in which case I'd strongly recommend making the view available to your loading API and passing the view to into which will handle sizing and cancellation for you.

Sam Judd
  • 7,017
  • 1
  • 33
  • 37
  • 1
    You can also call `preload()` instead of `into()` if you don't need the image immediately or if you'd rather use a `RequestListener` for the callback. – Jarett Millard Feb 05 '16 at 21:53
6

I use Glide 3.7.0 and download images this way:
it is important to note - it executes asyncroniously

Glide.with(this)
    .load(url)
    .downloadOnly(new SimpleTarget<File>() {
        @Override
        public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
            LOGGER.debug("Photo downloaded");
        }
    });

When I need to show cached image, I use the same url and DiskCacheStrategy.SOURCE:

Glide.with(this)
    .load(url)
    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
    .into(imageView);
Nik Kober
  • 770
  • 8
  • 13
1

It is now even simpler on Glide 4.9.0.

Glide.with(this)
    .downloadOnly()
    .load(url)
Carson Holzheimer
  • 2,309
  • 18
  • 33