回调奇怪的行为(Android,Picasso库)

时间:2014-05-24 08:43:36

标签: java android callback picasso

我正在使用Picasso库来管理我的图片上传和缓存。当我尝试执行此代码时:

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

我没有进入onBitmapLoaded回调。只有当我关闭活动(返回)并重新打开它时,我才会看到图像(进入onBitmapLoaded)。

但是,如果我只是在Toast回调中添加一些onPrepareLoad消息来更改我的代码,那么每件事都可以。这是完整的代码:

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

为什么Toast能让它发挥作用?这有什么问题?

1 个答案:

答案 0 :(得分:11)

我通过将Target实例声明为类成员来解决了这个问题。然后初始化它。像这样:

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