Digvijay
Digvijay

Reputation: 3271

Unable to cache images using Picasso

i am using Picasso as image loading library in my android project. I am using it in one fragment.At first it take some time to load profile image but when i open some other fragment and then come back to the same fragment.It again takes the same amount of time to load the same image.I think it is not caching image in the memory.

Below is my java code:

 Picasso.with(getContext()).load(str).fetch(new Callback() {
                      @Override
                      public void onSuccess() {

                          Picasso.with(getContext()).load(str).placeholder(R.drawable.user).fit().into(cv);
                      }

                      @Override
                      public void onError() {

                          TastyToast.makeText(getActivity(),"Unable to load profile image.", TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                      }
                  }); 

Upvotes: 2

Views: 237

Answers (3)

user10041244
user10041244

Reputation:

if your app is using internet connection to load image then every time you come back or go to your image containing fragment it will download image from internet so better you should store images in your app database and try to use thumbnails instead of actual images if possible it will take less time to get load

Upvotes: 0

Giulio Pettenuzzo
Giulio Pettenuzzo

Reputation: 806

try to take off the callback in order to know if the image is loaded, instead use the code below that automatically change the image in case of error

Picasso.with(getAplicationContext()).load("image to load").placeholder("image to show while loading").error("image in case of error").into(view);

Note the context of the first parameter: Picasso's cache still alive only if the context still alive, so if you put the ApplicationContext in the first parameter, the context and the cache keap alive even if the activity is changing. otherwise if you put getActivityContext() in the first parameter, the cache keap alive untill the activity hes been destroy

Upvotes: 0

theanilpaudel
theanilpaudel

Reputation: 3478

You have use it like this

Picasso.with(getContext()).load(str).networkPolicy(NetworkPolicy.OFFLINE).fetch(new Callback() {
                      @Override
                      public void onSuccess() {

                          //Picasso.with(getContext()).load(str).placeholder(R.drawable.user).fit().into(cv); don't need to use it here
                      }

                      @Override
                      public void onError() {

                          TastyToast.makeText(getActivity(),"Unable to load profile image.", TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                      }
                  }); 

Upvotes: 1

Related Questions