2

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();
                      }
                  }); 
Digvijay
  • 2,887
  • 3
  • 36
  • 86

3 Answers3

1

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();
                      }
                  }); 
theanilpaudel
  • 3,348
  • 8
  • 39
  • 67
  • I have tried above solution but it is showing Unable to load profile image toast. – Digvijay Jul 06 '18 at 08:32
  • do you know the url that is passed as str; the value of str – theanilpaudel Jul 06 '18 at 08:35
  • yes here str = https://firebasestorage.googleapis.com/v0/b/bookscomet-f4c4a.appspot.com/o/user.png?alt=media&token=6a6b81e2-c8ea-4799-af94-006adc198777 – Digvijay Jul 06 '18 at 08:45
  • can you set this "https://pbs.twimg.com/profile_images/875443327835025408/ZvmtaSXW_400x400.jpg" url in str and check if it is showing that image or not. Then we will know if it is the fault of url or not. – theanilpaudel Jul 06 '18 at 08:48
0

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

Giulio Pettenuzzo
  • 786
  • 1
  • 5
  • 20
0

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

  • How can i create thumbnail after downloading images . – Digvijay Jul 07 '18 at 07:27
  • to create thumbnail after downloading images see this link https://stackoverflow.com/questions/8557525/download-and-show-the-thumbnail –  Jul 08 '18 at 09:38