I\'m playing around with the Picasso library for image loading, but I\'m running into an issue. When an image fails to load, I want to hide the view rather than load in a de
Just a suggestion, but you might avoid issues in programming if you make an "empty" png file and set it as the default image file in your res folder... kinda silly i know... but likely to work without fighting...
When we got error, error goes to onError method then we handle it!
private void getAvatar(){
Picasso.with(this)
.load(Links.GET_AVATAR + ".jpg")
.into(imgUserAvatar, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
imgUserAvatar.setImageResource(R.drawable.icon_profile_default);
}
});
}
My answer:
File file = new File(filePath);
Picasso.with(context).load(file).placeholder(R.drawable.draw_detailed_view_display).error(R.drawable.draw_detailed_view_display)
.resize(400, 400).into(mImageView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
mImageView.setVisibility(View.GONE);
}
});
My example:
Picasso picasso = new Picasso.Builder(parent.getContext())
.listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
//Here your log
}
})
.build();
picasso.load(shopModel.getShopImg())
.fit()
.into(viewHolder.shopImg);
You can try to add a 'global' listener.
// create Picasso.Builder object
Picasso.Builder picassoBuilder = new Picasso.Builder(this);
picassoBuilder.listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("PICASSO", uri.toString(), exception);
}
});
// Picasso.Builder creates the Picasso object to do the actual requests
Picasso picasso = picassoBuilder.build();
try {
Picasso.setSingletonInstance(picasso);
} catch (IllegalStateException ignored) {
// Picasso instance was already set
// cannot set it after Picasso.with(Context) was already in use
}
Any subsequent calls to Picasso.with(Context context)
will return the instance which connected to listener, so all fails will be logged.
Please note that you need to call setSingletonInstance
as soon as possible, e.g. in Application onCreate
.
P.S. Code adopted from here - Customizing Picasso with Picasso.Builder
Picasso 2.0 allows you to attach a callback into a request.
https://github.com/square/picasso
The callback you are using is for "global" listener and it helps you debug errors that potentially happen due to a network load.
Use load(url).into(view, new Callback() {...});
in Picasso 2.0.
Remember to invoke cancelRequest(target)
if you are using a Callback
.