Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get Drawable image after picasso loaded

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.

like image 679
Shumin Avatar asked Sep 12 '14 02:09

Shumin


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.

Which is better glide or Picasso?

Glide's loading times are faster and it uses a small amount of memory for cache, but the library size is quite large. It, too, is easy to implement. Glide might be a better alternative to Picasso when memory footprint is less of a concern or more and larger images need to be processed.


2 Answers

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);
like image 196
Shane Avatar answered Oct 21 '22 15:10

Shane


        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() {
                                // ...
                            }
                        });
            }
        });
like image 2
Erlang Parasu Avatar answered Oct 21 '22 14:10

Erlang Parasu