java - Load big Images in Android -
it known android has issues storing bitmap data in ram memory. need load image (a photo 13mpx camera) on view , need able zoom-in , zoom-out image. image should mutable. implemented way:
bitmapfactory.options options = new bitmapfactory.options(); options.inmutable = true; _bitmap = bitmapfactory.decodefile(_path, options);
when take large photo (13 or 8 mpx) programm crushed "out of memory" error. need solution problem. need class can load , operate (scaling it) big images. it's needed equal or less api-8.
i tried universall image loader, has no scaling option. know ideas how solve problem?
a bitmap takes 4 bytes per pixel ==> 13mp equals 52mb of memory. should use bitmapfactory.options size first. using injustdecodebounds
bitmap object meta data, without actual image.
bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decoderesource(getresources(), r.id.myimage, options); int imageheight = options.outheight; int imagewidth = options.outwidth;
then calculate scale size screen needs:
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) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value power of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } return insamplesize; }
and use load scaled version of 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); }
this explained in developer guide
Comments
Post a Comment