Callback weird behaviour (Android, Picasso library

2019-05-19 07:53发布

I am using Picasso library to manage my image uploading and caching. When I am trying to execute this code:

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(new Target()
        {

            @Override
            public void onPrepareLoad(Drawable drawable) 
            {
            }

            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
            {
                cropImage(photo); //not getting here
            }

            @Override
            public void onBitmapFailed(Drawable arg0) 
            {
            }
        });  

I am not entering int the onBitmapLoaded callback. Only if I close the activity (going back) and re opening it I see the image (entering into onBitmapLoaded).

But if I will change my code by just adding some Toast message into the onPrepareLoad callback every thing works fine. here is the full code:

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(new Target()
        {

            @Override
            public void onPrepareLoad(Drawable drawable) 
            {
                Toast.makeText(thisActivity, "message", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
            {
                cropImage(photo);
            }

            @Override
            public void onBitmapFailed(Drawable arg0) 
            {
            }
        });

Why the Toast makes it work? what is wrong with it?

1条回答
在下西门庆
2楼-- · 2019-05-19 08:11

I solved the issue by declaring a Target instance as class member. then initialised it. Like this:

    target = new Target()
    {

        @Override
        public void onPrepareLoad(Drawable drawable) 
        {
        }

        @Override
        public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
        {
            cropEventImage(photo);
        }

        @Override
        public void onBitmapFailed(Drawable arg0) 
        {
        }
    };

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(target);
查看更多
登录 后发表回答