Thursday, 15 September 2011

android - Avoiding Out of Memory Error with Bitmap Factory, Even When Resizing Isn't an Issue? -



android - Avoiding Out of Memory Error with Bitmap Factory, Even When Resizing Isn't an Issue? -

i've researched @ to the lowest degree 10 similar topics on so, however, none have reached definitive reply me allowing me avoid out of memory error bitmaps known for.

taking consideration advice these previous questions, constructed next method setbipmapfrompath produce optimally sized (both in dimensions , kilobytes) wallpaper image file path. method works fine on big ram device g2, however, crashes in emulator 1.5gb of ram using 256kb picture.

i welcome criticism help me prevent out of memory error. hope ensure image can still deed proper background image, in, fill screen of device reasonably without insane stretch marks.

my methods:

public void recyclewallpaperbitmap() { if (mbitmap != null) { mbitmap.recycle(); mbitmap = null; } } private void setbitmapfrompath() { // recycle bitmap in case. recyclewallpaperbitmap(); string path = mprofilemanager.getwallpaperpath(); if (path != null) { windowmanager wm = (windowmanager)mcontext.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); int displaywidth = display.getwidth(); // deprecated int displayheight = display.getheight(); // deprecated final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = false; mbitmap = thumbnailutils.extractthumbnail(bitmapfactory.decodefile(path, options), displaywidth, displayheight); } }

this method works fine on big ram device g2, however, crashes in emulator 1.5gb of ram using 256kb picture.

it fail on g2 well, depending on , when phone call method. "256kb" image take several mb of heap space, , there no assurance have amount of heap space available in single contiguous block.

also, not utilize class , method designed creating thumbnails suitable creating wallpaper-sized images.

i welcome criticism help me prevent out of memory error.

use insamplesize on bitmapfactory.options downsample image closer right size while beingness read in.

then, dump utilize of thumbnailutils , allow imageview scale rest of way, avoid making yet re-create of image.

bonus points using inbitmap instead of junking , re-allocating bitmap every time, since screen size not changing, , hence wallpaper dimensions not changing.

these techniques , more covered in the developer documentation.

android bitmap window out-of-memory bitmapfactory

No comments:

Post a Comment