Android:毕加索加载图像失败。如何显示错误信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25744344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Picasso load image failed . how to show error message
提问by LittleFunny
I am trying to use the picasso library to loading the image store in the mediastore. When I called load(imageview, callback), the picasso call onFail instead of onSuccess. How do I know why the image was not loaded successfully?
我正在尝试使用 picasso 库在 mediastore 中加载图像存储。当我调用 load(imageview, callback) 时,毕加索调用 onFail 而不是 onSuccess。我怎么知道为什么图片没有成功加载?
回答by Kevin van Mierlo
Use builder:
使用构建器:
Picasso.Builder builder = new Picasso.Builder(this);
builder.listener(new Picasso.Listener()
{
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
{
exception.printStackTrace();
}
});
builder.build().load(URL).into(imageView);
Edit
编辑
For version 2.71828 they have added the exception to the onError callback:
对于 2.71828 版本,他们在 onError 回调中添加了异常:
Picasso.get()
.load("yoururlhere")
.into(imageView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
}
})
回答by TVT. Jake
When you use callback, the picaso will call method onSuccess and onError!
当您使用回调时,picaso 将调用方法 onSuccess 和 onError!
File fileImage = new File(mPathImage);
Picasso.with(mContext).load(fileImage)
.placeholder(R.drawable.draw_detailed_view_display)
.error(R.drawable.draw_detailed_view_display)
.resize(200, 200)
.into(holder.mImageEvidence, new Callback() {
@Override
public void onSuccess() {
holder.mMediaEvidencePb.setVisibility(View.GONE);
}
@Override
public void onError() {
holder.mErrorImage.setVisibility(View.VISIBLE);
}
});
回答by donfuxx
In case you want to use Picasso with Kotlin and lambda expression it could be as short as this:
如果您想将 Picasso 与 Kotlin 和 lambda 表达式一起使用,它可以像这样短:
val picasso = Picasso.Builder(context)
.listener { _, _, e -> e.printStackTrace() }
.build()
...and then you can load image as usual:
...然后你可以像往常一样加载图像:
picasso.load(url).into(imageView)