How to download and cache a bitmap using the Picasso library

I am using the following method

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

to download and receive images from a web page.

Does this method download an image from a URL every time, even if it is already loaded?

I want that after loading the image, and then next time, I have to get the image from the cache, no need to load.

If we have a method similar to the above requirement. Please let me know

+6
source share
2 answers

Does this method download an image from a URL every time, even if it is already loaded?

Not if it is cached.

The Picasso instance that you return with with() is preconfigured to have a memory cache and disk cache.

Depending on how much you load, you may run out of free space. And I hope Picasso uses things like ETag and If-Modified-Since to reload the image if the image has changed on the server, although I have not studied their code to make sure they do, as this behavior is not documented.

+2
source

Does this method download an image from a URL every time, even if it is already loaded? Not if it is cached.

According to the documentation and source code, Picasso does not cache anything when using the synchronous get () method.

So here is my solution for uploading an image synchronously and caching it using 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 } 
0
source

All Articles