网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载?
作者:互联网
我正在开发一个使用Picasso library在ViewPager和其他ImageView中加载图像的应用程序.所以我想知道如果关闭网络连接会发生什么.库会自行处理还是在将图像加载到视图之前检查网络连接?
我的代码:
Picasso picasso = Picasso.with(getActivity());
picasso.setDebugging(true);
picasso.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.into(eventImage, new Callback() {
@Override
public void onSuccess() {
Log.d("Success...", "picasso loaded successfully");
}
@Override
public void one rror() {
Log.d("Error...", "picasso load error");
}
});
解决方法:
使用以下代码,毕加索可缓存图像以供离线使用.
Picasso.with(this)
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//use this for offline support
.into(eventImage);
上面的代码在删除缓存时不起作用,因此毕加索无法从缓存中找到图像,如果没有从缓存中获取图像,我们将使图像在线并显示.我们使用以下代码来实现:
Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void one rror() {
Picasso.with(getActivity())
.load(downloadPath+imgDetailPhoto)
.placeholder(R.drawable.no_image)
.error(android.R.drawable.stat_notify_error)
.networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
.into(eventImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void one rror() {
//get error if image not loaded
}
});
}
});
标签:image-loading,picasso,offline-mode,android 来源: https://codeday.me/bug/20191027/1946116.html