【问题标题】:Callback weird behaviour (Android, Picasso library)回调怪异行为(Android、Picasso 库)
【发布时间】:2014-05-24 08:43:36
【问题描述】:

我正在使用 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)。

但是,如果我通过在 onPrepareLoad 回调中添加一些 Toast 消息来更改我的代码,那么一切正常。这是完整的代码:

    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 使它起作用?它有什么问题?

【问题讨论】:

  • 可能是onbitmapfailed被调用
  • 不是,调试器没有到达那里

标签: java android callback picasso


【解决方案1】:

我通过将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);

【讨论】:

  • 原因是你的Target 被垃圾回收了,因为毕加索持有一个弱引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 2013-05-22
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 2014-07-27
  • 2016-05-27
相关资源
最近更新 更多