Load images from disk cache with Picasso if offline

前端 未结 5 1738
轮回少年
轮回少年 2020-11-30 04:50

I have some images that I download from different web sites when the app starts, by doing this:

Picasso.with(context).load(image_url).fetch();
5条回答
  •  -上瘾入骨i
    2020-11-30 05:28

    You can use this code by this strategy Picasso will look for images in cache and if it failed only then image will be downloaded over network.

     Picasso.with(context)
                        .load(Uri.parse(getItem(position).getStoryBigThumbUrl()))
                        .networkPolicy(NetworkPolicy.OFFLINE)
                        .into(holder.storyBigThumb, new Callback() {
                            @Override
                            public void onSuccess() {
    
                            }
    
                            @Override
                            public void onError() {
                                // Try again online if cache failed
                                Picasso.with(context)
                                        .load(Uri.parse(getItem(position)
                                                .getStoryBigThumbUrl()))
                                .placeholder(R.drawable.user_placeholder)
                                .error(R.drawable.user_placeholder_error)
                                        .into(holder.storyBigThumb);
                            }
                        });
    

提交回复
热议问题