android - Bitmap caching doesn't work -
i'm trying cache downloaded bitmaps using google code it's not working me.
i download code article: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
and used in code, couldn't find example using guess may not using correctly.
in oncreate have line:
init(new imagecacheparams(this, this.getapplicationcontext().getcachedir().getpath()));
the init methods:
private void init(imagecacheparams cacheparams) { mcacheparams = cacheparams; // set memory cache if (mcacheparams.memorycacheenabled) { if (buildconfig.debug) { log.d(tag, "memory cache created (size = " + mcacheparams.memcachesize + ")"); } // if we're running on honeycomb or newer, if (utils.hashoneycomb()) { mreusablebitmaps = new hashset<softreference<bitmap>>(); } mmemorycache = new lrucache<string, bitmapdrawable>(mcacheparams.memcachesize) { /** * notify removed entry no longer being cached */ @override protected void entryremoved(boolean evicted, string key, bitmapdrawable oldvalue, bitmapdrawable newvalue) { if (recyclingbitmapdrawable.class.isinstance(oldvalue)) { // removed entry recycling drawable, notify // has been removed memory cache ((recyclingbitmapdrawable) oldvalue).setiscached(false); } else { // removed entry standard bitmapdrawable if (utils.hashoneycomb()) { // we're running on honeycomb or later, add bitmap // softrefrence set possible use inbitmap later mreusablebitmaps.add(new softreference<bitmap>(oldvalue.getbitmap())); } } } /** * measure item size in kilobytes rather units more practical * bitmap cache */ @override protected int sizeof(string key, bitmapdrawable value) { final int bitmapsize = getbitmapsize(value) / 1024; return bitmapsize == 0 ? 1 : bitmapsize; } }; } // default disk cache not initialized here should initialized // on separate thread due disk access. if (cacheparams.initdiskcacheoncreate) { // set disk cache new initializescache().execute(); } } class initializescache extends asynctask<string, void, void> { protected void doinbackground(string... args) { initdiskcache(); return null; } public void initdiskcache() { // set disk cache synchronized (mdiskcachelock) { if (mdisklrucache == null || mdisklrucache.isclosed()) { file diskcachedir = mcacheparams.diskcachedir; if (mcacheparams.diskcacheenabled && diskcachedir != null) { if (!diskcachedir.exists()) { diskcachedir.mkdirs(); } if (getusablespace(diskcachedir) > mcacheparams.diskcachesize) { try { mdisklrucache = disklrucache.open( diskcachedir, 1, 1, mcacheparams.diskcachesize); if (buildconfig.debug) { log.d(tag, "disk cache initialized"); } } catch (final ioexception e) { mcacheparams.diskcachedir = null; log.e(tag, "initdiskcache - " + e); } } } } mdiskcachestarting = false; mdiskcachelock.notifyall(); } }
now use line when download bitmap own db:
addbitmaptocache("foo", *some drawable*);
the method:
public void addbitmaptocache(string data, bitmapdrawable value) { if (data == null || value == null) { return; } // add memory cache if (mmemorycache != null) { if (recyclingbitmapdrawable.class.isinstance(value)) { // removed entry recycling drawable, notify // has been added memory cache ((recyclingbitmapdrawable) value).setiscached(true); } mmemorycache.put(data, value); } synchronized (mdiskcachelock) { // add disk cache if (mdisklrucache != null) { final string key = hashkeyfordisk(data); outputstream out = null; try { disklrucache.snapshot snapshot = mdisklrucache.get(key); if (snapshot == null) { final disklrucache.editor editor = mdisklrucache.edit(key); if (editor != null) { out = editor.newoutputstream(disk_cache_index); value.getbitmap().compress( mcacheparams.compressformat, mcacheparams.compressquality, out); editor.commit(); out.close(); } } else { snapshot.getinputstream(disk_cache_index).close(); } } catch (final ioexception e) { log.e(tag, "addbitmaptocache - " + e); } catch (exception e) { log.e(tag, "addbitmaptocache - " + e); } { try { if (out != null) { out.close(); } } catch (ioexception e) {} } } } }
after try bitmap calling this:
tempimage.setimagedrawable(getbitmapfrommemcache("foo")); public bitmapdrawable getbitmapfrommemcache(string data) { bitmapdrawable memvalue = null; if (mmemorycache != null) { memvalue = mmemorycache.get(data); } if (buildconfig.debug && memvalue != null) { log.d(tag, "memory cache hit"); } return memvalue; }
when using flow don't see bitmap don't errors.
i tried @ other git codes doing cache codes not clear how use, lack of example or none description @ all.
use library android universal image loader loading , caching bitmaps
Comments
Post a Comment