3

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.

Eric Brandwein
  • 861
  • 8
  • 23

2 Answers2

1

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

Community
  • 1
  • 1
Eric Brandwein
  • 861
  • 8
  • 23
0

Use Glide instead, it provide predefine constrain options which you can add with it

https://github.com/bumptech/glide

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Matt Ke Jun 24 '20 at 12:04