Reputation: 391
How do I lazily load images from JSON to preview in an Android GridView? A full example would be appreciated.
Upvotes: 0
Views: 527
Reputation: 1478
You can get the image url from JSON and use an ASyncTask to download images. If you want, you can pass the view to asynctask and from it's postexecute, you can set its image source
Upvotes: 0
Reputation: 133560
You needparse your json response get the url of images. Then use a grdiview with a custom adapter. Pass the url to the custom adapter constructor.
For parsing json use gson
https://code.google.com/p/google-gson/
Tutorial for the same
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
You can use Lazy List or Universal ImageLoader.
Images can be cached to local sd card or phone mmeory. Url is considered the key. If the key is present in sdcard display images from sd card else display image by downloading from server and cache the same to location of your choice. The cache limit can set. You can also choose your own location to cache images. Cache can also be cleared.
Instead of user waiting to download large images and then displaying lazy list loads images on demand. Since images area cached you can display images offline.
https://github.com/thest1/LazyList. Lazy List
In your getview
imageLoader.DisplayImage(imageurl, imageview);
ImageLoader Display method
public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key
if(bitmap!=null) //if image exists
imageView.setImageBitmap(bitmap); //dispaly iamge
else //downlaod image and dispaly. add to cache.
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
} An alternative to Lazy List is Universal Image Loader
https://github.com/nostra13/Android-Universal-Image-Loader. It is based on Lazy List(works on same principle). But it has lot of other configurations. I would prefer to use Universal Image Loader coz it gives you more configuration options. You can display a error image if downlaod failed. Can display images with rounded corners. Can cache on disc or memory. Can compress image.
In your custom adapter constructor
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
You can configure with other options to suit your needs.
Along with lazy loading/Universal Image Loader you can view holder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
Upvotes: 0
Reputation: 16613
I recommend using the Universal Image Loader for loading of the images and gson library for creating objects from JSON.
Upvotes: 1