Skip to content Skip to sidebar Skip to footer

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.draw

Solution 1:

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);

Solution 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() {
                                // ...
                            }
                        });
            }
        });

Post a Comment for "Android Get Drawable Image After Picasso Loaded"