i using imageloader class load images url. images storing in gallery folder name called lazylist. takes upto 40 -100 mb of memory. dont want load images since users may feel discomfort. sorry bad english.
all works fine creates folder in gallery , shows images using app. feel users feel discomfort app.
here code of imageloader , links few other classes even
public class imageloader { memorycache memorycache = new memorycache(); filecache filecache; private map<imageview, string> imageviews = collections .synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; public imageloader(context context) { filecache = new filecache(context); executorservice = executors.newfixedthreadpool(5); } final int stub_id = r.drawable.abs__ab_bottom_transparent_light_holo; public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap = memorycache.get(url); if (bitmap != null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(stub_id); } } private void queuephoto(string url, imageview imageview) { phototoload p = new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f = filecache.getfile(url); // sd cache bitmap b = decodefile(f); if (b != null) return b; // web try { bitmap bitmap = null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection) imageurl .openconnection(); conn.setconnecttimeout(30000); conn.setreadtimeout(30000); conn.setinstancefollowredirects(true); inputstream = conn.getinputstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); bitmap = decodefile(f); return bitmap; } catch (exception ex) { ex.printstacktrace(); return null; } } // decodes image , scales reduce memory consumption private bitmap decodefile(file f) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); // find correct scale value. should power of 2. final int required_size = 100; 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; return bitmapfactory.decodestream(new fileinputstream(f), null, o2); } catch (filenotfoundexception e) { } return null; } // task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i) { url = u; imageview = i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload) { this.phototoload = phototoload; } @override public void run() { if (imageviewreused(phototoload)) return; bitmap bmp = getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if (imageviewreused(phototoload)) return; bitmapdisplayer bd = new bitmapdisplayer(bmp, phototoload); activity = (activity) phototoload.imageview.getcontext(); a.runonuithread(bd); } } boolean imageviewreused(phototoload phototoload) { string tag = imageviews.get(phototoload.imageview); if (tag == null || !tag.equals(phototoload.url)) return true; return false; } // used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p) { bitmap = b; phototoload = p; } public void run() { if (imageviewreused(phototoload)) return; if (bitmap != null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(stub_id); } } public void clearcache() { memorycache.clear(); filecache.clear(); } }
i got solution changing filecache
class , destination folder
public class filecache { private file cachedir; private file nomediafile; string nomedia = " .nomedia"; public filecache(context context) { // find dir save cached images if (android.os.environment.getexternalstoragestate().equals( android.os.environment.media_mounted)) { cachedir = new file(environment.getexternalstoragedirectory() + "/mydir"); if (cachedir.mkdir()) { nomediafile = new file( environment.getexternalstoragedirectory() + "/mydir/" + nomedia); if (!nomediafile.exists()) { try { nomediafile.createnewfile(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } else { cachedir = context.getcachedir(); } if (!cachedir.exists()) cachedir.mkdirs(); } public file getfile(string url) { // identify images hashcode. not perfect solution, // demo. // string filename=string.valueof(url.hashcode()); // possible solution (thanks grantland) @suppresswarnings("deprecation") string filename = urlencoder.encode(url); file f = new file(cachedir, filename); return f; } public void clear() { file[] files = cachedir.listfiles(); if (files == null) return; (file f : files) f.delete(); } }
Comments
Post a Comment