How to access Drawable when using Picasso?

Henrique picture Henrique · Sep 4, 2013 · Viewed 13.7k times · Source

I'm using the Picasso framework to handle image loading in my Android app. After the image is loaded, I need to access the Drawable to apply some masking operations. The issue is that Picasso converts the Drawable to a PicassoDrawable, and a simple cast back to Drawable does not work.

This is the code I have:

        Picasso.with(mContext).load(image.getPath()).into(mImageView, new Callback() {

            @Override
            public void onSuccess() {

                Util.applyMask(imageView);
            }

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

and the Util.applyMask(ImageView) method:

public static void applyMask(ImageView imageView) {

    // this is where a class cast exception happens since it's actually a PicassoDrawable and not a Drawable
    Bitmap mainImage = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

// ... 
}

A possible solution is given by Jake Wharton in this github issue: https://github.com/square/picasso/issues/38

To sum up, the solution is: "If you want access to the Bitmap directly then you'll need to use the Target callbacks. The PicassoDrawable is used to allow fading and the debug indicator."

I'm not exactly sure how to access the Target callback. Anyone knows how to solve this?

Thanks.

Answer

Henrique picture Henrique · Sep 4, 2013

This was answered at github (https://github.com/square/picasso/issues/38):

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

      @Override
      public void onBitmapFailed() {
      }
    }

private void loadBitmap() {
   Picasso.with(this).load("url").into(target);
}