Search code examples
javaandroidkotlinandroid-glide

How to log each request url from Glide?


I have using Glide to download images for quite a long time, so the code like this are every where:

      GlideApp.with(getContext())
            .load(imgUrl)
            .into(imgView);

And suddenly I need to log every image's url for further analysis. Instead of searching every usage of Glade and changing the code like this:

      GlideApp.with(getContext())
            .load(imgUrl)
            .listener(requestListener) // log urls through listener
            .into(imgAd);

is there any way that I could add the requestListener globally like in the default options of Glide so that I don't need to search and change every Glide usage throughout the application?

I use Glide v4 and I have checked the post which log the urls directly to logcat but not to my local storage for further analysis.


Solution

  • Glide 4.9.0 can set default requestListener.

    link https://github.com/bumptech/glide/releases/tag/v4.9.0 https://github.com/bumptech/glide/commit/37127f0f817d4a11dfdcc447946397b5288de593

    In Custom AppGlideModule

    @GlideModule
    public class MyAppGlideModule extends AppGlideModule {
    
        @Override
        public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
            builder.addGlobalRequestListener(new RequestListener<Object>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Object> target, boolean isFirstResource) {
                    return false;
                }
    
                @Override
                public boolean onResourceReady(Object resource, Object model, Target<Object> target, DataSource dataSource, boolean isFirstResource) {
                    return false;
                }
            });
        }
    }
    

    In Custom Activity Fragment

    GlideApp.with(object).addDefaultRequestListener()

    Glide.with(object).addDefaultRequestListener()