【发布时间】:2016-11-08 00:32:27
【问题描述】:
我正在尝试将图像加载到我的视图中。有时它起作用,有时它不起作用。我正在 API 级别 19 模拟器上进行测试。新目标内部的故障块永远不会被调用。我看到prepareLoad,然后:
-
调用
onBitmapLoaded,图片会显示 - 图片不会显示
为什么会这样?
这发生在模拟器上。在物理设备上,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");
}
});
}
【问题讨论】: