android caching image from sdcard -
i want image sdcard in listview item able show image want cache image have smooth scroll can please tell me how cache image sdcard got code show image drawable want sdcard
import java.lang.ref.weakreference; import java.util.arraylist; import android.content.context; import android.content.res.resources; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.os.asynctask; import android.util.lrucache; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridlayout.layoutparams; import android.widget.gridview; import android.widget.imageview; public class listadapter extends baseadapter { context context; arraylist<string> items; private lrucache<string, bitmap> mmemorycache; public listadapter(context context, arraylist<string> items) { this.context = context; this.items = items; // memory class of device, exceeding amount throw // outofmemory exception. final int maxmemory = (int) (runtime.getruntime().maxmemory() / 1024); // use 1/8th of available memory memory cache. final int cachesize = maxmemory / 8; mmemorycache = new lrucache<string, bitmap>(cachesize) { protected int sizeof(string key, bitmap bitmap) { // cache size measured in bytes rather number // of items. return bitmap.getbytecount(); } }; } @override public int getcount() { return items.size(); } @override public object getitem(int arg0) { return items.get(arg0); } @override public long getitemid(int arg0) { return arg0; } @override public view getview(int arg0, view convertview, viewgroup arg2) { imageview img = null; if (convertview == null) { img = new imageview(context); img.setscaletype(imageview.scaletype.center_crop); img.setlayoutparams(new gridview.layoutparams( layoutparams.match_parent, layoutparams.match_parent)); } else { img = (imageview) convertview; } int resid = context.getresources().getidentifier(items.get(arg0), "drawable", context.getpackagename()); loadbitmap(resid, img); return img; } public void loadbitmap(int resid, imageview imageview) { if (cancelpotentialwork(resid, imageview)) { final bitmapworkertask task = new bitmapworkertask(imageview); imageview.setbackgroundresource(r.drawable.empty_photo); task.execute(resid); } } static class asyncdrawable extends bitmapdrawable { private final weakreference<bitmapworkertask> bitmapworkertaskreference; public asyncdrawable(resources res, bitmap bitmap, bitmapworkertask bitmapworkertask) { super(res, bitmap); bitmapworkertaskreference = new weakreference<bitmapworkertask>( bitmapworkertask); } public bitmapworkertask getbitmapworkertask() { return bitmapworkertaskreference.get(); } } public static boolean cancelpotentialwork(int data, imageview imageview) { final bitmapworkertask bitmapworkertask = getbitmapworkertask(imageview); if (bitmapworkertask != null) { final int bitmapdata = bitmapworkertask.data; if (bitmapdata != data) { // cancel previous task bitmapworkertask.cancel(true); } else { // same work in progress return false; } } // no task associated imageview, or existing task // cancelled return true; } private static bitmapworkertask getbitmapworkertask(imageview imageview) { if (imageview != null) { final drawable drawable = imageview.getdrawable(); if (drawable instanceof asyncdrawable) { final asyncdrawable asyncdrawable = (asyncdrawable) drawable; return asyncdrawable.getbitmapworkertask(); } } return null; } public void addbitmaptomemorycache(string key, bitmap bitmap) { if (getbitmapfrommemcache(key) == null) { mmemorycache.put(key, bitmap); } } public bitmap getbitmapfrommemcache(string key) { return (bitmap) mmemorycache.get(key); } class bitmapworkertask extends asynctask<integer, void, bitmap> { public int data = 0; private final weakreference<imageview> imageviewreference; public bitmapworkertask(imageview imageview) { // use weakreference ensure imageview can garbage // collected imageviewreference = new weakreference<imageview>(imageview); } // decode image in background. @override protected bitmap doinbackground(integer... params) { data = params[0]; final bitmap bitmap = decodesampledbitmapfromresource( context.getresources(), data, 100, 100); addbitmaptomemorycache(string.valueof(params[0]), bitmap); return bitmap; } // once complete, see if imageview still around , set bitmap. @override protected void onpostexecute(bitmap bitmap) { if (imageviewreference != null && bitmap != null) { final imageview imageview = imageviewreference.get(); if (imageview != null) { imageview.setimagebitmap(bitmap); } } } } public static bitmap decodesampledbitmapfromresource(resources res, int resid, int reqwidth, int reqheight) { // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decoderesource(res, resid, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; return bitmapfactory.decoderesource(res, resid, options); } public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { // calculate ratios of height , width requested height , // width final int heightratio = math.round((float) height / (float) reqheight); final int widthratio = math.round((float) width / (float) reqwidth); // choose smallest ratio insamplesize value, // guarantee // final image both dimensions larger or equal // requested height , width. insamplesize = heightratio < widthratio ? heightratio : widthratio; } return insamplesize; } }
i think have use aquery. download aquery jar file here. add build path , want show image, display below:
aquery aquery=new aquery(this); file file = new file(your image path); aq.id(r.id.avatar).image(file, true,true); // both true means caching images.
Comments
Post a Comment