gpt4 book ai didi

android - 使用 Picasso 加载图像并不总是显示

转载 作者:搜寻专家 更新时间:2023-11-01 08:31:20 24 4
gpt4 key购买 nike

我正在尝试将图像加载到我的 View 中。有时有效,有时无效。我正在 API 级别 19 模拟器上进行测试。永远不会调用新目标内的失败 block 。我看到 prepareLoad 然后是:

  1. onBitmapLoaded 被调用,图像将显示
  2. 图片不会显示

为什么会这样?

这是在模拟器上发生的。在物理设备上,Q&A 报告了 100% 的失败率。在其他设备上,我看到间歇性故障率。这是怎么回事?

public void setBackground() {
final LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);

final Context context = this;
final String imagePath = getStore().backgroundImageURI;

if (getStore().backgroundImageNumber > 0) {
mainLayout.setBackground(context.getResources().getDrawable(getStore().backgroundImageNumber));
return;
}
if (imagePath == null) {
mainLayout.setBackground(context.getResources().getDrawable(R.drawable.sk_collection_bg_default));
return;
}
Picasso.with(this).load(imagePath).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
Log.v("Biscuit-width", String.valueOf(bitmap.getWidth()));
Log.v("Biscuit-height", String.valueOf(bitmap.getHeight()));

mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}

@Override
public void onBitmapFailed(final Drawable errorDrawable)
{
Log.d("BISCUIT", "FAILED" + errorDrawable.toString());

}

@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
}

最佳答案

我有一段时间没有使用 Picasso,但在过去,Targets 是 WeakReferences,你必须保持对它的硬引用(如果情况不再如此,请原谅我,但 Jake Wharton 非常坚定关于“你必须保留一个硬引用,否则目标将被收集为垃圾”的事情;可能是因为他被问到同样的事情超过 9000 次(包括我自己)。

所以看看这个堆栈溢出响应似乎是同一个问题......

https://stackoverflow.com/a/26918731/2684

As noted by the other respondents (@lukas and @mradzinski), Picasso only keeps a weak reference to the Target object. While you can store a strong reference Target in one of your classes, this can still be problematic if the Target references a View in any manner, since you'll effectively also be keeping a strong reference to that View as well (which is one of the things that Picasso explicitly helps you avoid).

If you are in this situation, I'd recommend tagging the Target to the View.

关于android - 使用 Picasso 加载图像并不总是显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40477071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com