14

I am using Picasso library to load image from url. The code I used is below.

Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder).into(imageView);

What I wanna do is to get the image that loaded from url. I used

Drawable image = imageView.getDrawable();

However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.

Thanks in advance.

Shumin
  • 991
  • 2
  • 13
  • 22

2 Answers2

25

This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:

   Target target = new Target() {
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
              imageView.setImageBitmap(bitmap);
              Drawable image = imageView.getDrawable();
          }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {}

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {}
   };

   Picasso.with(this).load("url").into(target);
Shane
  • 972
  • 2
  • 12
  • 27
  • How can I use loadBitmap()? I need to call Picasso twice? – Shumin Sep 12 '14 at 02:59
  • No that is just a void method you would call to load the image. You don't need it. (see updated answer) – Shane Sep 12 '14 at 03:01
  • 1
    this is useless, `fit, centercrop` won't work, what the point in using Picasso then?? you can get drawable with normal Picasso callback: `into(targetImageView, new Callback() { @Override public void onSuccess() { targetImageView.getDrawable(); }` – user924 Sep 23 '17 at 10:26
2
        mImageView.post(new Runnable() {
            @Override
            public void run() {
                mPicasso = Picasso.with(mImageView.getContext());
                mPicasso.load(IMAGE_URL)
                        .resize(mImageView.getWidth(), mImageView.getHeight())
                        .centerCrop()
                        .into(mImageView, new com.squareup.picasso.Callback() {
                            @Override
                            public void onSuccess() {
                                Drawable drawable = mImageView.getDrawable();
                                // ...
                            }

                            @Override
                            public void onError() {
                                // ...
                            }
                        });
            }
        });
Erlang Parasu
  • 183
  • 2
  • 3
  • 8