How do I lazily load images from JSON to preview in an Android GridView? -


how lazily load images json preview in android gridview? full example appreciated.

you needparse json response url of images. use grdiview custom adapter. pass url custom adapter constructor.

for parsing json use gson

https://code.google.com/p/google-gson/

tutorial same

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

you can use lazy list or universal imageloader.

images can cached local sd card or phone mmeory. url considered key. if key present in sdcard display images sd card else display image downloading server , cache same location of choice. cache limit can set. can choose own location cache images. cache can cleared.

instead of user waiting download large images , displaying lazy list loads images on demand. since images area cached can display images offline.

https://github.com/thest1/lazylist. lazy list

in getview

imageloader.displayimage(imageurl, imageview); imageloader display method  public void displayimage(string url, imageview imageview) //url , imageview parameters { imageviews.put(imageview, url); bitmap bitmap=memorycache.get(url);   //get image cache using url key if(bitmap!=null)         //if image exists     imageview.setimagebitmap(bitmap);  //dispaly iamge  else   //downlaod image , dispaly. add cache.  {     queuephoto(url, imageview);     imageview.setimageresource(stub_id);  } 

} alternative lazy list universal image loader

https://github.com/nostra13/android-universal-image-loader. based on lazy list(works on same principle). has lot of other configurations. prefer use universal image loader coz gives more configuration options. can display error image if downlaod failed. can display images rounded corners. can cache on disc or memory. can compress image.

in custom adapter constructor

 file cachedir = storageutils.getowncachedirectory(a, "your folder");  // singletone instance of imageloader imageloader = imageloader.getinstance(); // create configuration imageloader (all options optional) imageloaderconfiguration config = new imageloaderconfiguration.builder(a)       // can pass own memory cache implementation      .disccache(new unlimiteddisccache(cachedir)) // can pass own disc cache implementation      .disccachefilenamegenerator(new hashcodefilenamegenerator())      .enablelogging()      .build(); // initialize imageloader created configuration. once. imageloader.init(config); options = new displayimageoptions.builder() .showstubimage(r.drawable.stub_id)//display stub image .cacheinmemory() .cacheondisc() .displayer(new roundedbitmapdisplayer(20)) .build(); 

in getview()

 imageview image=(imageview)vi.findviewbyid(r.id.imageview);   imageloader.displayimage(imageurl, image,options);//provide imageurl, imageview , options 

you can configure other options suit needs.

along lazy loading/universal image loader can view holder smooth scrolling , performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -