Question

I am using the following method

Bitmap bitmap = Picasso.with(ListofCardsActivity.this)
                            .load(overLayUrl).get();

for downloading and get the image from the web url.

Does this method download the image from the url every time, even if it is downloaded already?

What I want is that once the image is downloaded, then from the next time onwards, I should get the image from the cache, no need to download.

If we have the method like the above requirement. please let me know

Was it helpful?

Solution

Does this method download the image from the url every time, even if it is downloaded already?

Not if it is cached.

The Picasso instance you get back with with() is pre-configured to have a memory cache and a disk cache.

Depending on how much you are downloading, you may run out of cache space. And I would hope that Picasso uses stuff like ETag and If-Modified-Since to re-download the image if the image has changed on the server, though I have not examined their code to see if they do, as that behavior is not documented.

OTHER TIPS

Does this method download the image from the url every time, even if it is downloaded already? Not if it is cached.

According to the documentation and the source code, Picasso doesn't cache anything when using synchronous get() method.

So here is my solution for loading image synchronously and caching it with Picasso:

    File fileImage = new File("/path/to/your/image");

    final Bitmap[] bmpRes = new Bitmap[1];
    final Semaphore semaphore = new Semaphore(0);
    Picasso.with(this).load(fileImage).priority(Picasso.Priority.HIGH).into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            bmpRes[0] = bitmap;
            semaphore.release();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            semaphore.release();
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    });
    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if(bmpRes[0] != null) {
        Bitmap bmp = bmpRes[0];
        //TODO: Whatever you want with the bitmap
    } else {
        //TODO: Failed to load the image
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top