Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Drawable when using Picasso?

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.

like image 221
Henrique Avatar asked Sep 04 '13 13:09

Henrique


People also ask

How will you load an image into an imageView from an image URL using Picasso and display a custom image if there is an error while loading the image?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details.


1 Answers

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);
}
like image 158
Henrique Avatar answered Oct 06 '22 09:10

Henrique