Hassel-free Image Loading Libraries

In this part, we are going to talk about some the best Image Loading Libraries for loading images.

It is always not about downloading image from remote location. You also have to think of implementing image caching logic in order to provide a seamless user experience.

Many a times we may have to load multiple images say 50+ with bigger size (640×380) asynchronously in List View or Gird View which might result in OutOfMemory error. Even i have faced this issue and have tried to various ways to fix this, but was not so effective.After doing lot of research i found that there are libraries which will load images asynchronously, and cache images as well.

1) Picasso 

  • Picasso simplifies the process of loading images from external urls and display on your application.
  •  Automatic image caching.
  • Handling Image View recycling.
  • Efficient Memory usage with minimal memory use, also takes care of Out of Memory Exception.
  • Automatic job cancellation in lists where views are re-used.
  • It even does “Image Fade In” which is popular now a days.
  • Batching UI Updates and improve UI responsiveness.
  • Debug indicators to check if its from Network,Disk or Cache.

 Usage :

In Adapter’s getView() :

Picasso.with(mContext).load(Url)
.placeholder(R.drawable.place_holder)
.error(R.drawable.error_image).tag(mContext).into(imageView);

Resource Loading :

Resources, assets, files, content providers are all supported as image sources.

Picasso.with(context).load(R.drawable.image).into(imageView1);
Picasso.with(context).load(“file:///android_asset/my_image.png”).into(imageView2);
Picasso.with(context).load(new File(…)).into(imageView3);

 Read More :

http://square.github.io/picasso/

2) Volley : 

  • Developed and Maintained by Android Dev Team.
  • Asynchronous Networking Library.
  • NetworkImageView is meant to be a direct replacement for the standard ImageView in scenarios when your image resource is coming from a network location (i.e. URL).

Usage :

<ImageView
    android:id=”@+id/image_view”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_alignParentBottom=”true”
    android:layout_alignParentTop=”true”
   />

Becomes :

<com.android.volley.toolbox.NetworkImageView
android:id=”@+id/image_view”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentTop=”true”
/>

Setting up the Image Loader Class :

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

In constructor or onCreate() initialize the objects :

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});

Loading an Image into Image View :

NetworkImageView networkImageView = (NetworkImageView)view.findViewById(R.id.image_view);
networkImageView.setImageUrl(“http://yoururl.com/image.png&#8221;,mImageLoader);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s