1

Picasso does not call onBitmapLoaded for the first time ,if you know, please tell me

        txtView = (TextView) centerRelative.getChildAt(i);
                Picasso.with(getBaseContext()).load(file[i-4]).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        txtView.setBackground(new BitmapDrawable(getResources(),bitmap));
//                      Not executing for the first time

                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {
//                      executing for the first time
                    }
                });

Target target = new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        txtView.setBackground(new BitmapDrawable(getResources(),bitmap));
                        logd("onBitmapLoaded");
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                };
                Picasso.with(getBaseContext()).load(file[i-4]).into(target);
Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
initialise
  • 211
  • 1
  • 3
  • 11

1 Answers1

5

Solution: You have to make some changes here:

Instead of writing:

new Target() {...}

inside your into(..), you must create a global object of Target class. Do not make it local object as it may be garbage collected. So,

Step1:

Make a Global object:

Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        ....
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        ....
    }
    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        ....
    }
};

Finally, Use that Target In your into(...):

Picasso.with(getBaseContext()).load(file[i-4]).into(target);

Try it, Hope it helps.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41