android - OutofMemory Error when loading Images in Gridview -
i'm implementing following code in adapter when faced problem of outofmemory error.but problem here able load first enter code here image 0th position in gridview. below code, please suggest changes.
public class gridadapter extends baseadapter { string[] names; int[] images; context context; private static layoutinflater inflater=null; //private lrucache<string bitmap="",> memorucache; private lrucache<string, bitmap> mmemorycache; public gridadapter(context mainactivity,string[] _names,int[] _images){ names=_names; images=_images; context=mainactivity; inflater= (layoutinflater) context.getsystemservice(context.layout_inflater_service); final int maxmemory= (int) (runtime.getruntime().maxmemory() / 1024); final int cachesize=maxmemory/8; mmemorycache = new lrucache<string, bitmap>(cachesize) { @override protected int sizeof(string key, bitmap bitmap) { // cache size measured in kilobytes rather // number of items. return bitmap.getbytecount() / 1024; } }; } @override public int getcount() { return names.length; } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { holder holder=new holder(); view rowview; rowview=inflater.inflate(r.layout.gridlist,null); holder.txtview= (textview) rowview.findviewbyid(r.id.textview1); holder.imageview= (imageview) rowview.findviewbyid(r.id.imageview1); holder.txtview.settext(names[position]); loadbitmap(images[position], holder.imageview); //holder.imageview.setimageresource(images[position]); //rowview.setbackgroundcolor(color.parsecolor(getcolorcode())); return rowview; } private void loadbitmap(int image, imageview mimageview) { final string imagekey=string.valueof(image); final bitmap bitmap=getbitmapfrommemcache(imagekey); if(bitmap != null){ mimageview.setimagebitmap(bitmap); }else{ mimageview.setimageresource(r.drawable.ic_launcher); final bitmapworkertask task=new bitmapworkertask(mimageview); task.execute(image); } } public class holder{ textview txtview; imageview imageview; } public string getcolorcode(){ string[] colors = {"#6600cc","#3399ff","#ff9900","#003399","#cc6600" ,"#336600","#339933","#009999","#99cc00","#666633" ,"#666699","#333399","#003399","#993399","#990033"}; int random = (int)(math.random()*14+1); return colors[random]; } class bitmapworkertask extends asynctask<integer,void,bitmap>{ private final weakreference<imageview> imageviewreference; public bitmapworkertask(imageview mimageview) { imageviewreference = new weakreference<imageview>(mimageview);; } @override protected bitmap doinbackground(integer... params) { final bitmap bitmap = decodesampledbitmapfromresource( context.getresources(), params[0], 100, 100); addbitmaptomemorycache(string.valueof(params[0]), bitmap); return bitmap; //return null; } } public void addbitmaptomemorycache(string key, bitmap bitmap) { if (getbitmapfrommemcache(key) == null) { mmemorycache.put(key, bitmap); } } public bitmap getbitmapfrommemcache(string key) { return mmemorycache.get(key); } 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; } return insamplesize; } }
your images large loaded in memory. following steps:
- first try resample images , load images using universal image loader https://github.com/nostra13/android-universal-image-loader try to
- load original images universal image loader, https://github.com/nostra13/android-universal-image-loader . uses caching mecanism.
hope helps.
Comments
Post a Comment