Image downloading and caching with Picasso Library

Picasso Library is very powerful tool for avoiding Bitmap handling in your application. It takes care of displaying image efficiently in ImageView from local sdcard, network or application resource. All of this can be done in single line of code.


If you are a self-do android programmer, you will take care of same task writing several lines of code.

1. Using AsyncTask for downloading images in background.

2. In addition to background thread, you will keep track of downloads for particular ImageView in order to handle orientation change or if user scrolls your list/grid view.

3. To avoid downloading image, you would prefer to have cache for your downloaded bitmaps. You will implement LRUCache<String, Bitmap> for smooth caching of downloaded images.

Picasso library provides you power to handle all of the above task with just single line of code. It will take care of following functionalities by itself.

1. Handling ImageView recycling and download cancellation in an adapter.

2. Complex image transformations with minimal memory use. You can resize your bitmaps to fit nicely in your ImageView without any hassle.

3. Automatic memory and disk caching.

[su_button url=”https://github.com/androidsrc/PicassoSample” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_b[/su_button]p style="text-align: justify;">

 Sample Picasso Application

Picasso_ImageLoad

 

1. Add gradle dependency in build.gradle file.
compile 'com.squareup.picasso:picasso:2.5.2'

2. Add Internet permission in application manifest file since we will load image from network as well.
<uses-permission android:name="android.permission.INTERNET"/>

3. Prepare your layout view for showing images loaded by Picasso. I have taken 3 ImageViews to show loading images from network, application resource and sdcard. Modify your /res/layout/activity_main.xml as below.

You may also like...

1 Response

  1. Brian Quinn says:

    I operate on a dimmed light bulb now and then. You mentioned that Picasso automatically handles caching (I believe with LruCache). Is Picasso saving images to LruCache and retrieving them to bind to the adapter to get the Bitmap into the image view? We don’t have to code add and get to LruCache ourselves?

Leave a Reply

Your email address will not be published. Required fields are marked *