我正在使用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能让它发挥作用?这有什么问题?
答案 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);