网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载? [英] Does Picasso library for Android handle image loading while network connectivity is off?

查看:70
本文介绍了网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Picasso库的应用程序在我的ViewPager和其他ImageViews.所以我想知道如果关闭网络连接会发生什么.库会自行处理还是在将图像加载到视图之前检查网络连接?

I'm working on an application in which i use Picasso library for image loading in my ViewPager and other ImageViews. So i want to know what happens if network connectivity is off. Does the library handle itself or do i have to check the network connectivity before loading image to views?

我的代码:

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 onError() {
                        Log.d("Error...", "picasso load error");

                    }
                });

推荐答案

使用下面的代码,毕加索缓存图像以供离线使用.

Using below code Picasso caches images for offline use.

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);

以上代码在删除缓存时不起作用,因此毕加索无法从缓存中查找图像.如果没有从缓存中获取图像,我们将在线获取并显示图像.我们使用以下代码实现了这一点:

Above code is not worke while removing cache.so Picasso can't find image from cache.If not get image from cache we handle to get image online and display it. We achieve that using below code:

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 onError() {
          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 onError() {
           //get error if image not loaded
        }
    });
}
});

这篇关于网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆