O
O
Oleg Gamega2014-11-24 18:18:00
Android
Oleg Gamega, 2014-11-24 18:18:00

Picasso how to cache pictures correctly?

Good day.
It is necessary to display pictures in the listview (about 186 kb each).
Pictures are loaded from the server, uploaded using picasso, cached using OkHttp

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.albums_item, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) view.findViewById(R.id.image);
            viewHolder.progress = (ProgressBar) view.findViewById(R.id.progressBar);
            view.setTag(viewHolder);
        } else {
            view = convertView;
        }
        final ViewHolder holder = (ViewHolder) view.getTag();
        holder.progress.setVisibility(View.VISIBLE);
        Picasso picasso;
        OkHttpClient okHttpClient;

        okHttpClient = new OkHttpClient();
        picasso = new Picasso.Builder(mContext)
                .downloader(new OkHttpDownloader(okHttpClient))
                .build();
        picasso.with(mContext)
                .load(getItem(position).getMain_photo_url())
                .into(holder.image, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.progress.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError() {

                    }
                });
        return view;
    }

I need to ensure that the feed is like in Instagram ─ what has already loaded does not go anywhere, but when I scroll, the imageview is cleared, caching did not help (moreover, it does not really work) when the net is disabled, nothing is loaded from the cache

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Keyn Din, 2014-11-26
@Lure_of_Chaos

Use weak references like WeakHaskMap

3
321SKART123, 2015-11-24
@321SKART123

Picasso caches the image at the http level. Check the headers that the server that hosts the image gives (the keywords "Cache-Control", "public, max-age, max-stale")
If the server gives with a short "life" time, then theoretically it is possible, by inheriting the standard picasso http client, to slip the desired header - but it's hard. In general, the author himself answered this on the stack - look
. Also, you need to check the size of the disk cache and the cache in the RAM for picasso. But that's probably not your problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question