Picasso — Catching and Analyzing Errors

When working with Picasso, at some point you'll run into the following scenario: Picasso shows the error placeholder and you can't figure out why. Even when you activate logging you'll only see that loading an image failed, but not the reason for the failure.

In this tutorial, you'll learn how you can use a custom instance of the Picasso builder to access the root causes for failed loading processes.

Picasso Series Overview

Picasso Builder for Customization

We won't cover the details of customizing Picasso with the builder again, so catch up with the linked tutorial, if you haven't used the builder so far.

The Picasso builder allows us to set a listener for failed image loading processes. Thus, when you're trying to load an image with this custom Picasso instance, the callback will deliver you the error message when something goes wrong:

// create Picasso.Builder object
Picasso.Builder picassoBuilder = new Picasso.Builder(context);

// Picasso.Builder creates the Picasso object to do the actual requests
// add a listener for error callbacks
Picasso picasso = picassoBuilder.listener(new Picasso.Listener() {  
    @Override
    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
        // log errors
    }
}).build();

In the callback you've three parameters: the Picasso instance, the attempted Uri and finally the exception. You can use this information to figure out why an image couldn't be loaded.

Of course, the action you want to take depends on your app scenario: do you want to log the error to a central bug tracking service? Or analyze the exception for better user feedback? It's up to you.

Summary

In this tutorial you've learned how you can gain access to image loading errors. Sometimes it's necessary to know why an image failed (and not just that it failed). The Picasso builder's listener() option provides this functionality to you.

Do you have further questions on this topic or about Picasso in general? Just let us know on Twitter @futurestud_io or leave a comment below.

Enjoy coding & make it rock!

Explore the Library

Find interesting tutorials and solutions for your problems.