Callback weird behaviour (Android, Picasso library)

混江龙づ霸主 提交于 2019-12-22 12:27:21

问题


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:


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


来源:https://stackoverflow.com/questions/23843055/callback-weird-behaviour-android-picasso-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!