android - Grid view crashe the app on scrolling on too many images -
in project uses custom adapter grid view , when grid view load many items contain images on scrolling activity crashes , reloads again. had resource problem on loading image solved of @raghunandan error out of memmory. cause think loadfull size images.
public class myadapter extends arrayadapter<structurecase> { private layoutinflater minflater = null; public context context; public class distinationactivity = null; public myadapter(context context, int textviewresourceid, list<structurecase> objects) { super(context, textviewresourceid, objects); minflater = layoutinflater.from(context); // minflater = (layoutinflater)g.currentactivity.getsystemservice(context.layout_inflater_service); } public static class viewholder { public imageview gem_img = null; public textview gem_name = null; } @override public view getview(int position, view convertview, viewgroup parent) { final viewholder viewholder; final view v; final structurecase item = getitem(position); if (convertview == null) { convertview = this.minflater.inflate(r.layout.my_grid_list, null); //minflater = (layoutinflater)g.currentactivity.getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.my_grid_list, parent, false); viewholder = new viewholder(); convertview.settag(viewholder); viewholder.gem_img = (imageview) convertview.findviewbyid(r.id.imageview_mygrid_list); viewholder.gem_name = (textview) convertview.findviewbyid(r.id.textview_mygrid_list); viewholder.gem_name.settypeface(g.typefaceprs); } else { viewholder = (viewholder) convertview.gettag(); } int temp = 0; viewholder.gem_name.settext(item.g_name); int id = g.currentactivity.getresources().getidentifier(item.g_image, "drawable", g.currentactivity.getpackagename()); drawable drawable = g.currentactivity.getresources().getdrawable(id); if(drawable != null){ viewholder.gem_img.setimagedrawable(drawable); }else{ viewholder.gem_img.setimageresource(r.drawable.almas); } //viewholder.newsthumb.setimageresource(temp); viewholder.gem_img.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); return convertview; } @override public long getitemid(int position) { return position; }
use code in getview method
while setting bitmap in imageview
.
private bitmap decodeuri(uri selectedimage) throws filenotfoundexception { bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream( getcontentresolver().openinputstream(selectedimage), null, o); final int required_size = 800; 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; } bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale;`enter code here` return bitmapfactory.decodestream( getcontentresolver().openinputstream(selectedimage), null, o2); }
Comments
Post a Comment