Load images from disk cache with Picasso if offline

前端 未结 5 1659
轮回少年
轮回少年 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条回答
  • 2020-11-30 05:26

    Do this:

    Picasso.with(this)
                .load(url)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .into(imageView);
    

    Also check my previous answer, maybe will help you: Invalidate cache in Picasso

    0 讨论(0)
  • 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);
                            }
                        });
    
    0 讨论(0)
  • 2020-11-30 05:30

    To avoid creating of separate instance of RequestCreator like here , do this:

     RequestCreator request = mPicasso.load(TextUtils.isEmpty(imageUrl) ? null : imageUrl)
                        .placeholder(R.drawable.ic_default)
                        .error(R.drawable.ic_default)
                        .transform(transformations)
                        .noFade()
                        .centerCrop();
                request
                        .networkPolicy(NetworkPolicy.OFFLINE)
                        .into(mImageView, new Callback.EmptyCallback(){
                            @Override
                            public void onError() {
                                super.onError();
                                request.into(mImageView);
                            }
                        });
    
    0 讨论(0)
  • 2020-11-30 05:40

    This logic worked for me:

    if network is available:
        Picasso.with(context).load(image).into(imageView);
    else:
        Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(imageView);
    
    0 讨论(0)
  • 2020-11-30 05:41

    Is OkHttp and Okio present on the class path? (or in your dependencies) Because by default Picasso lets the HttpClient handle the caching (it does not do this by default)

    You have 2 options

    • include the mentioned dependencies (recommended)
    • specify the cache manually
    0 讨论(0)
提交回复
热议问题