B
B
BitNeBolt2020-05-02 10:55:24
Android
BitNeBolt, 2020-05-02 10:55:24

Why can't I load a picture into the cache (Picasso)?

In RecyclerView, you need to load images along the paths that are stored in the database. When loading large images (3000 * 4000), the list is initialized with lags. To avoid this, I decided to try loading images from the cache using the following. the code:

LruCache cache = new LruCache(contex);
        if (cache.get(imgPath) != null)
        {
            //Если картинка есть в кэше
            holder.image.setImageBitmap(cache.get(imgPath));
            Log.d("Picture ", "Picture is in cache");
        }
        else
        {
            try
            {
                //если картинки нету в кэше
                Picasso.get().load(new File(imgPath).getAbsoluteFile()).into(holder.image);
                cache.set(imgPath, BitmapFactory.decodeFile(new File(imgPath).getAbsolutePath()));
                Log.d("Picture ", "Picture is not in cache");
            }
            catch (java.lang.NullPointerException e)
            {
                Log.d("Picture", "Failed to load picture from this path");
                Log.d("Picture", "cache size: " + cache.size());
            }
        }


But, judging by the logs, the picture is never added to the cache.

Is this the correct way to download? Will this solve the problem with loading large images?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-05-02
@BitNeBolt

No need for this perversion. Picasso should have its own cache, use it. If it is not there, change the picassa to glide.
And, in any case, 3k x 4k pictures are a tin. Glyde will resize them on the go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question