Loading remote images from an url into ImageView
s in Android is one of the features that I have used quite extensively for almost all the Android apps that I have worked on. This isn’t really trivial as loading remote images requires creating an asynchronous task to download the image from url and then load it into the image view. There are several things that need to be taken care of when working with remote images because you can never know how large the image will be and loading a very big image into memory will cause the application to run out of memory very quicky.
While working on a project that relied heavily on external images, I tried out several of available libraries (UrlImageViewHelper, UniversalImageLoader and Picasso) to come up with the one that performs the best.
UrlImageViewLoader is a really light weight library and it works out of the box. All you need is to import the project into Eclipse, link it as a library project and start loading images into ImageViews with UrlImageViewHelper.setUrlDrawable(imageView, "https://example.com/image.png");
. It performs callback methods, caching and works with ListAdapters. But this is where the fun stops. There are not a lot of configuration options but is really easy to quickly get started with.
Picasso is equally simple to use and performs advanced features like image cropping, resizing and transformations. It makes it very easy to resize the images if you already know the size of the image view you are loading the images into and the performance starts showing when you use resizing while loading a lot of images in a list view. This was something that I was using and Picasso immediately had an edge over UrlImageViewLoader for my requirements. The fact that it was developed by Jake Wharton (the developer of ActionBarSherlock) made it that much more appealing. But there were a lot of quirks and resizing didn’t seem to work always. It is still in very active development with a few commits at least every few days, but I would still wait for it to be more stable before trying it out again.
The final library in my review is UniversalImageLoader which is available as a jar file that can just be dropped into the project libraries. It is considerably heavy as compared with the other two libraries in the review, but has a lot of configuration options that allow controlling just about anything with the remote image loading. One of the most important things about it is the integrated resizing of images based on a maximum size (maybe not as efficient as Picasso, but definitely stable). The library has several caching strategies for the images as well as several display options. However, it does require a bit more configuration on the part of the developer. Here are the configuration options that I ended up using with UniversalImageLoader in my Application class.
// Create global configuration and initialize ImageLoader
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.EXACTLY)
.showStubImage(R.drawable.image_placeholder).showImageForEmptyUri(R.drawable.image_placeholder).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions).build();
ImageLoader.getInstance().init(config);