Wednesday, 15 February 2012

Android BitmapFactory.decodeFile slows down until out of memory -



Android BitmapFactory.decodeFile slows down until out of memory -

i processing 1200 images. optimized work 100 images 500 help of previous questions found here. now, have:

public bitmap getbitmap(string filepath) { boolean done = false; int downsampleby = 2; bitmap bitmap = null; bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(filepath, options); // decode bitmap insamplesize set options.injustdecodebounds = false; options.inpreferredconfig = config.rgb_565; while (!done) { options.insamplesize = downsampleby++; seek { bitmap = bitmapfactory.decodefile(filepath, options); done = true; } grab (outofmemoryerror e) { // ignore. seek again. } } homecoming bitmap; }

this function called in loop, , goes fast until hits 500th image. @ point slows down, until stops working @ around 600th image.

at point don't know how else optimize create work. think happening , how can prepare it?

edit

// decode bitmap considering memory limitations public bitmap getbitmap(string filepath) { bitmap bitmap = null; bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(filepath, options); // decode bitmap insamplesize set options.injustdecodebounds = false; options.inpreferredconfig = config.rgb_565; options.indither = true; options.insamplesize= calculateinsamplesize(options, 160, 120); homecoming bitmap = bitmapfactory.decodefile(filepath, 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) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value powerfulness of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } homecoming insamplesize; }

made changes of accepted answer. using function google's tutorials right sample size. added largeheap in manifest , calling system.gc() 1 time before loop through images.

first of all, should never expect grab error. described here: java documentation an error subclass of throwable indicates serious problems reasonable application should not seek catch.

there help loading bitmaps: android developers | loading big bitmaps

you can more memory declaring largeheap="true" attribute in application manifest.

and system.gc() phone call might help freeing unused memory, won't rely on call.

android bitmapfactory

No comments:

Post a Comment