android - Downloading images in list view -
android - Downloading images in list view -
i downloading images server using lazy loading. images downloaded in cache not appear in list view appear when scroll list view or calling list view sec time
public class imageloader { //the simplest in-memory cache implementation. should replaced softreference or bitmapoptions.inpurgeable(since 1.6) public static hashmap<string, bitmap> cache=new hashmap<string, bitmap>(); private imageview imageview; private file cachedir; private context context; public imageloader(context context){ this.context=context; //make background thead low priority. way not impact ui performance photoloaderthread.setpriority(thread.norm_priority-1); //find dir save cached images if (android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted)) cachedir=new file(android.os.environment.getexternalstoragedirectory(),"ikzoook"); else cachedir=context.getcachedir(); if(!cachedir.exists()) cachedir.mkdirs(); } final int stub_id= r.drawable.imgbgg; public void displayimage(string url, activity activity, imageview imageview) { this.imageview=imageview; if(cache.containskey(url)) imageview.setimagebitmap(cache.get(url)); else { queuephoto(url, activity, imageview); imageview.setimageresource(stub_id); } } private void queuephoto(string url, activity activity, imageview imageview) { //this imageview may used other images before. there may old tasks in queue. need discard them. photosqueue.clean(imageview); phototoload p=new phototoload(url, imageview); synchronized(photosqueue.photostoload){ photosqueue.photostoload.push(p); photosqueue.photostoload.notifyall(); } //start thread if it's not started yet if(photoloaderthread.getstate()==thread.state.new) photoloaderthread.start(); } private bitmap getbitmap(string url) { //i identify images hashcode. not perfect solution, demo. string filename=string.valueof(url.hashcode()); file f=new file(cachedir, filename); //from sd cache bitmap b = decodefile(f); if(b!=null) homecoming b; //from web seek { bitmap bitmap=null; inputstream is=new url(url).openstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); bitmap = decodefile(f); if(bitmap==null){ log.d("nullllllllllll","nulllllllllllllllllllllll"); } homecoming bitmap; } grab (exception ex){ // imageview.setimageresource(r.drawable.business); ex.printstacktrace(); //bitmap bmap = bitmapfactory.decodefile("r.drawable.business"); bitmap viewbgrnd = bitmapfactory.decoderesource(this.context.getresources(), r.drawable.imgbgg); homecoming viewbgrnd; } } //decodes image , scales cut down memory consumption private bitmap decodefile(file f){ seek { //decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f),null,o); //find right scale value. should powerfulness of 2. final int required_size=70; int width_tmp=o.outwidth, height_tmp=o.outheight; int scale=1; while(true){ if(width_tmp/2<required_size || height_tmp/2<required_size) break; width_tmp/=2; height_tmp/=2; scale*=2; } //decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize=scale; homecoming bitmapfactory.decodestream(new fileinputstream(f), null, o2); } grab (filenotfoundexception e) {} homecoming null; } //task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i){ url=u; imageview=i; } } photosqueue photosqueue=new photosqueue(); public void stopthread() { photoloaderthread.interrupt(); } //stores list of photos download class photosqueue { private stack<phototoload> photostoload=new stack<phototoload>(); //removes instances of imageview public void clean(imageview image) { for(int j=0 ;j<photostoload.size();){ if(photostoload.get(j).imageview==image) photostoload.remove(j); else ++j; } } } class photosloader extends thread { public void run() { seek { while(true) { //thread waits until there images load in queue if(photosqueue.photostoload.size()==0) synchronized(photosqueue.photostoload){ photosqueue.photostoload.wait(); } if(photosqueue.photostoload.size()!=0) { phototoload phototoload; synchronized(photosqueue.photostoload){ phototoload=photosqueue.photostoload.pop(); } bitmap bmp=getbitmap(phototoload.url); cache.put(phototoload.url, bmp); object tag=phototoload.imageview.gettag(); if(tag!=null && ((string)tag).equals(phototoload.url)){ bitmapdisplayer bd=new bitmapdisplayer(bmp, phototoload.imageview); activity a=(activity)phototoload.imageview.getcontext(); a.runonuithread(bd); } } if(thread.interrupted()) break; } } grab (interruptedexception e) { //allow thread exit } } } photosloader photoloaderthread=new photosloader(); //used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; imageview imageview; public bitmapdisplayer(bitmap b, imageview i){bitmap=b;imageview=i;} public void run() { if(bitmap!=null){ bitmap bit=bitmap.createbitmap(bitmap); int width = bitmap.getwidth(); int height = bitmap.getheight(); float scalewidth = ((float)100) / width; float scaleheight = ((float)100) / height; // create matrix manipulation matrix matrix = new matrix(); // resize bit map matrix.postscale(scalewidth, scaleheight); bitmap resizedbitmap = bitmap.createbitmap(bit, 0, 0, width, height, matrix, false); //img.setimagebitmap(resizedbitmap); imageview.setimagebitmap(resizedbitmap); } else{ log.d("hellooooooooooooooooooo","hellooooooooooooooo"); imageview.setimageresource(r.drawable.imgbgg); } } } public void clearcache() { //clear memory cache cache.clear(); //clear sd cache file[] files=cachedir.listfiles(); for(file f:files) f.delete(); } }
you can after images downloaded finish
adapter.notifydatasetchanged(); android image android-listview
Comments
Post a Comment