1

In my app I want to download images from url and show them in recyclerView. And basically everything is ok - when I download images, turn off wifi and mobile data - cached images are being displayed. Tried several times - it works perfectly. However ... after for example 3-4 hrs I tried again to launch app in offline mode and images were not displayed. Any idea what I'm doing wrong ? Here's my code in basic activity (onCreate) :

Picasso.Builder builder = new Picasso.Builder(getContext());
        builder.downloader(new OkHttp3Downloader(getContext(),Integer.MAX_VALUE));
        Picasso built = builder.build();
        built.setIndicatorsEnabled(true);
        built.setLoggingEnabled(true);

Then I download and cache images like this :

public void onResponse(Call<CallbackListPost> call, Response<CallbackListPost> response) {
                CallbackListPost resp = response.body();
                if (resp != null && resp.status.equals("ok")) {
                    post_total = resp.count_total;
                    displayApiResult(resp.posts);
                    controller = new RealmController(getActivity().getApplication());
                    controller.savePost(resp.posts);
                    for(Post post : resp.posts) {
                        for (final Attachment att : post.attachments) {
                            Picasso.with(getActivity())
                                    .load(att.url)
                                    .fetch(new com.squareup.picasso.Callback() {
                                        @Override
                                        public void onSuccess() {
                                            Log.e(TAG, "onSuccess: " + att.url );
                                        }

                                        @Override
                                        public void onError() {

                                        }
                                    });
                        }
                    }

Then in second activity I display images like this :

public View getView(int position, View convertView, ViewGroup parent) {


            ImageView iv;

            if(convertView == null) {
                iv = new ImageView(mContext);
            } else {
                iv = (ImageView) convertView;
            }

            Picasso.with(mContext)
                    .load(att.get(position).url)
                    .noFade()
                    .resize(150,150)
                    .centerCrop()
                    .into(iv);
                return iv;
        }
Bartos
  • 1,007
  • 3
  • 15
  • 38

3 Answers3

2

Hello you can try this:

Picasso.with(mContext)
                .load(att.get(position).url)
                .noFade()
                .resize(150,150)
                .centerCrop()
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(iv);
Tony Barajas
  • 124
  • 2
  • 12
1

I don't have much knowledge about Picasso. I have a suggestion that you must go with Glide, an image caching tool. Its very small library and very fast.

Simple use cases with Glide's generated API will look something like this:

// For a simple view:

@Override public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  GlideApp.with(this).load("goo.gl/gEgYUd").into(imageView);
}

// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
  } else {
    myImageView = (ImageView) recycled;
  }

  String url = myUrls.get(position);

  GlideApp
    .with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .into(myImageView);

  return myImageView;
}

It has auto cache config, which means we don't need to bother about the image cache.It loads perfectly after first use.

In Glide V4

Glide.with(context)
    .load(“/user/profile/photo/path”)
    .asBitmap()
    .toBytes()
    .centerCrop()
    .into(new SimpleTarget<byte[]>(250, 250) {
        @Override
        public void onResourceReady(byte[] data, GlideAnimation anim) {
            // Post your bytes to a background thread and upload them here.
        }
    });
Varun Chandran
  • 647
  • 9
  • 23
0

If you refer to the documentation of the latest version of Picasso (2.71828) and figure out how Picasso handles it internally, you will understand why your images are NOT getting loaded from Disk/Cache.

Well explained answer is here https://stackoverflow.com/a/57114949/1508631.

SS06Dec86
  • 209
  • 4
  • 11