Glide — Exceptions: Debugging and Error Handling

After introducing all fundamental concepts of Glide, we'll move to a developer topic today. In this tutorial, we'll show you a few helpful ways to debug issues you might experience during the image loading process with Glide.

Glide Series Overview

Local Debugging

Glide's GeneralRequest class offers a method to set the log level. Unfortunately, you don't have easy access to the class in production use. Nevertheless, there is a very simple way to get Glide's debug logs. All you've to do is to activate it via the adb shell. Open your terminal and use the following command:

adb shell setprop log.tag.GenericRequest DEBUG  

The last part DEBUG comes from the standard Android log constants. Thus, your options with increasing priority as the parameter are:

  • VERBOSE
  • DEBUG
  • INFO
  • WARN
  • ERROR

The output, in case of a non-existing image, would look like this:

io.futurestud.tutorials.glide D/GenericRequest: load failed  
io.futurestud.tutorials.glide D/GenericRequest: java.io.IOException: Request failed 404: Not Found  
...

As you already guessed, this only works when you have physical access to the device and while you're developing and testing your app. For logging in production app, you'll need a different way. The answer is once again callbacks, which we'll explore in the next section.

General Exception Logging

Glide doesn't offer direct access to the GenericRequest class to set the logging, but you can catch the exception in case something goes wrong with the request. For example, if an image is not available, Glide would (silently) throw an exception and show the drawable you've specified in .error(). If you explicitly want to know the exception, create a listener and pass it to the .listener() method on the Glide builder.

First, create the listener as a field object to avoid that it gets garbage collected:

Glide 4.x

private RequestListener<Bitmap> requestListener = new RequestListener<Bitmap>() {  
  @Override
  public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
    // todo log exception to central service or something like that

    // important to return false so the error placeholder can be placed
    return false;
  }

  @Override
  public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
    // everything worked out, so probably nothing to do
    return false;
  }
};

Glide 3.x

private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {  
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        // todo log exception

        // important to return false so the error placeholder can be placed
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        return false;
    }
};

In the onLoadFailed (Glide 3.x: onException) method, you can catch the problem and decide what you need to do, for example log it. It's important that you return false in the method, if Glide should handle the consequences, like displaying an error placeholder, itself.

You can set the listener on the Glide builder:

Glide 4.x

GlideApp  
    .with(context)
      .asBitmap()
      .load(UsageExampleListViewAdapter.eatFoodyImages[0])
      .listener(requestListener)
      .error(R.drawable.cupcake)
      .into(imageViewPlaceholder);

Glide 3.x

Glide  
    .with( context )
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .listener( requestListener )
    .error( R.drawable.cupcake )
    .into( imageViewPlaceholder );

The .error() is not required to make the logging work. However, the R.drawable.cupcake gets only displayed, if you return false in the onException method of the listener.

Outlook

In this tutorial, you've learned some ways to debug and log exceptions that might occur when working with Glide. Choose one of the ways, depending on your need. If you've questions, let us know in the comments!

In the next tutorial, we'll move towards more advanced topics. Our topic in the coming week is custom transformations!

Explore the Library

Find interesting tutorials and solutions for your problems.