Android Bitmap OutOfMemoryError -
this question has answer here:
i'm having outofmemoryerror in vsd220 (it's 22" android based in one)
for (imageview img : listimages) { system.gc(); bitmap mybitmap = bitmapfactory.decodefile(path); img.setimagebitmap(mybitmap); img.setonclicklistener(this); }
i don't know do, because image below maximum resolution. image size (1000x1000), , display it's 1920x1080.
any help? (that foreach cycle 20 elements, gots broken after 6, or 7 loops..)
thanks lot.
ezequiel.
you should take @ training docs managing bitmap memory. depending on os version, use different techniques allow manage more bitmaps, you'll have change code anyway.
in particular, you're going have use amended version of code in "load scaled down version memory", @ least have found section particularly useful:
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; } 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); }
this method makes easy load bitmap of arbitrarily large size imageview displays 100x100 pixel thumbnail, shown in following example code:
mimageview.setimagebitmap( decodesampledbitmapfromresource(getresources(), r.id.myimage, 100, 100));
Comments
Post a Comment