Search code examples
androidpicasso

Picasso not loading bitmap into imageview with target


I'm trying to load an url into an ImageView but Target doesn't seem to work like what I found told me. Below is my code:

ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
setMethodPicture(sectionModel.getContent().get(0).getThumbnail(), methodButton);
methodsContainer.addView(methodButton);

and

private void setMethodPicture(final String methodPicture, final ImageView methodButton){
    methodButton.setBackgroundColor(0x000000);
    if (!StringUtils.isEmpty(methodPicture)) {
        Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                methodButton.setImageBitmap(bitmap);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }

            @Override
            public boolean equals(Object o) {
                return methodPicture.equals(o);
            }

            @Override
            public int hashCode() {
                return methodPicture.hashCode();
            }
        };
        Picasso.with(context).load(methodPicture).into(target);
    }
}

This doesn't load the picture to the imageView, but when i do this,

ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
methodButton.setBackgroundColor(0x000000);
Picasso.with(context).load(sectionModel.getContent().get(0).getThumbnail()).into(methodButton);
methodsContainer.addView(methodButton);

it loads the picture. I want to do the first one so I can change the Bitmap I get before I put it in the ImageView, like changing the width and height, but basing on the original dimensions.


Solution

  • I found the issue, Picasso holds a Weak Reference to the Target. The correct answer can be found here: onBitmapLoaded of Target object not called on first load