How to listen for Picasso (Android) load complete events?

JavaAndroidPicasso

Java Problem Overview


Is there a way to listen for events from Picasso when using the builder like:

Picasso.with(getContext()).load(url).into(imageView);

I'm trying to call requestLayout() and invalidate() on the parent GridView so it'll resize properly but I don't know how to set a listener or callback.

I see that Picasso has error event reporting, but is there a success event?

Java Solutions


Solution 1 - Java

You can use a Callback to get onSuccess and onError events. Just add a new Callback to your request like so:

Picasso.with(getContext())
    .load(url)
    .into(imageView, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                        }
                    });

Then you can perform any alterations and modifications in the onSuccess callback.

Solution 2 - Java

If you need to access the bitmap before it is loaded to the view, try using :

private Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {       
      }
      @Override
      public void onBitmapFailed() {
      }
}

In the calling method:

Picasso.with(this).load("url").into(target);

Ideally you'd implement Target on a view or view holder object directly.

Hope this helps

Solution 3 - Java

Answering @Jas follow up question as a comment to MrEngineer13's answer (since I don't have enough reputation to comment in any answer), you should use the error() method prior to registering the Callback at the into() method, for example:

Picasso.with(getContext())
    .load(url)
    .error(R.drawable.error_placeholder_image)
    .into(imageView, new com.squareup.picasso.Callback() {
        @Override
        public void onSuccess() {
            //Success image already loaded into the view
        }

        @Override
        public void onError() {
            //Error placeholder image already loaded into the view, do further handling of this situation here
        }
    }
);

Solution 4 - Java

Square lately has updated Target class and now there are more methods to override (onPrepareLoad and onBitmapFailed):

Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};

And you still have to use:

Picasso.with(context).load(url).into(target);

Solution 5 - Java

 private final Callback mImageCallback = new Callback() {
        @Override
        public void onSuccess() {
            startPostponedEnterTransition();
        }

        @Override
        public void onError() {
            startPostponedEnterTransition();
        }
    };

RequestCreator creator = Picasso.with(getActivity()).load(list.get(position).getId());
creator.into(imageView, mImageCallback);

Solution 6 - Java

Try This

       Picasso.with(context)
            .load(services.get(position).getImageInactive())
            .into(holder.icon, new Callback() {
                @Override
                public void onSuccess() {
                    holder.imageLoad.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    holder.icon.setImageResource(R.drawable.ic_error_image_load);
                }
            });

Solution 7 - Java

As a complement to other answers, in case you don't know where to use original image view, e.g. ImageView myIV:

Original:

Picasso.with(activity).load(url).into(myIV);

New (inside onBitmapLoaded() of new Target()):

public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    myIV.setImageBitmap(bitmap);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKeithView Question on Stackoverflow
Solution 1 - JavaMrEngineer13View Answer on Stackoverflow
Solution 2 - JavaAndroidmeView Answer on Stackoverflow
Solution 3 - JavaGuilherme SantosView Answer on Stackoverflow
Solution 4 - JavaMateusz PryczkowskiView Answer on Stackoverflow
Solution 5 - Javavaibhav jainView Answer on Stackoverflow
Solution 6 - JavaA.G.THAMAYSView Answer on Stackoverflow
Solution 7 - Java林果皞View Answer on Stackoverflow