android - LRU caching of images in gridview is not working -


ive read , watched every possible tutorial caching of image couldnt find solution this. using async tesk load images gridview cells.

1-inside gridview of adapter checking if bitmap found in cache before calling loading task.

2-in asynctask im saving loaded bitmap cache. cache empty bitmaps not saved. im posting adapter , asynctask. note: used exact tutorial of android developers

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#config-changes in advance

    public class fragmentcelladapter extends baseadapter {        public fragmentcelladapter(context context,string json,int test){     mcontext=context;         this.json = json;  //something use in code      s = test;      try {         this.jsonarray = new jsonarray(json);     } catch (jsonexception e) {         e.printstacktrace();     }      final int maxmemory = (int) (runtime.getruntime().maxmemory()/1024 ); // use 1/8th of available memory memory cache. final int cachesize = maxmemory / 8;     log.e("maxmemory , cach ",""+maxmemory +" "+ cachesize);    memorycache =   new lrucache<string,bitmap>(cachesize){         @override         protected int sizeof(string key, bitmap value) {             log.e("size of returns "," " + bitmap.getbytecount()/1024);             return bitmap.getbytecount();         }     };  }         @override public long getitemid(int position) {     return 0; }  @override public object getitem(int position) {     log.e("calling item of grid adpter","at position " + position);     return null; }  @override public int getcount() {     return jsonarray.length(); }  @override public view getview(final int position, view convertview, viewgroup parent) {     layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);     view view;            view = inflater.inflate(r.layout.listfragmentcell,null);          cellim = (imageview) view.findviewbyid(r.id.cellimage2);           final  bitmap bitmap1 = getbitmapfrommemcache(imgurl);             if (bitmap1 == null){                 new loadimage(cellim,b).execute("http://restaurantapp.azurewebsites.net/images/menuitems/"+image);                 log.e("null bitmap calling task  @ position ", + position+      " : " +"http://restaurantapp.azurewebsites.net/images/menuitems/"+image);                 }             else {                  cellim.setimagebitmap(bitmap1);log.e("bitmap found in ","                               cache") ;}       return view;    }         public void addbitmaptomemorycache(string key, bitmap bitmap) {   if (getbitmapfrommemcache(key) == null) {     log.e("adding image url",key);     memorycache.put(key, bitmap);     }   }      public bitmap getbitmapfrommemcache(string key) {     if (memorycache.get(key) == null){log.e("null @ key",key);}     return memorycache.get(key);    }         public class loadimage extends asynctask<string,void,bitmap>{      weakreference<imageview> imageviewreference;     weakreference<progressbar> pbar;      public loadimage (imageview im) {         this.imageviewreference = new weakreference<imageview>(im);        // this.pbar = new weakreference<progressbar>(bar);     }       @override     protected bitmap doinbackground(string... params) {          try {              url url = new url(params[0]);              httpurlconnection connection = (httpurlconnection)           url.openconnection();              int responsecode = connection.getresponsecode();             log.e("code of res"," " + responsecode);             if (responsecode == httpstatus.sc_ok){             inputstream in = connection.getinputstream();              bitmap = bitmapfactory.decodestream(in);              matrix matrix = new matrix();             matrix.postscale(50,50);            resizedbitmap  = bitmap.createbitmap(bitmap,0,0,100,150);              // adding bitmaps cache             addbitmaptomemorycache(string.valueof(url),resizedbitmap);}             else {log.e("response code ",responsecode + "");}           } catch (ioexception e) {             e.printstacktrace();         }           return resizedbitmap;     }      @override     protected void onpostexecute(bitmap bitmapp) {         super.onpostexecute(bitmapp);          imageview imageview = imageviewreference.get();         progressbar bar = pbar.get();          if (imageview != null){             bar.setvisibility(view.gone);             imageview.setimagebitmap(bitmapp);}      } 

the problem everytime getview() calls task because bitmaps in cache null. again

do not reinvent wheel. creating caching library not trivial task @ all. use developed library(picasso, glide) or @ least read sources.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -