How do I use disk caching in Picasso?

前端 未结 9 793
-上瘾入骨i
-上瘾入骨i 2020-11-22 14:44

I am using Picasso to display image in my android app:

/**
* load image.This is within a activity so this context is activity
*/
public void loadImage (){
           


        
9条回答
  •  心在旅途
    2020-11-22 15:38

    I use this code and worked, maybe useful for you:

    public static void makeImageRequest(final View parentView,final int id, final String imageUrl) {
    
        final int defaultImageResId = R.mipmap.user;
        final ImageView imageView = (ImageView) parentView.findViewById(id);
        Picasso.with(context)
                .load(imageUrl)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .into(imageView, new Callback() {
                    @Override
                    public void onSuccess() {
                    Log.v("Picasso","fetch image success in first time.");
                    }
    
                    @Override
                    public void onError() {
                        //Try again online if cache failed
                        Log.v("Picasso","Could not fetch image in first time...");
                        Picasso.with(context).load(imageUrl).networkPolicy(NetworkPolicy.NO_CACHE)
                                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).error(defaultImageResId)
                                .into(imageView, new Callback() {
    
                                    @Override
                                    public void onSuccess() {
                                        Log.v("Picasso","fetch image success in try again.");
                                    }
    
                                    @Override
                                    public void onError() {
                                      Log.v("Picasso","Could not fetch image again...");
                                    }
    
                                });
                    }
                });
    
    }
    

提交回复
热议问题