Android out of memory error on choosing second image -
my app works stepping out of main activity, starts second activity, displays image chosen user , analyzes image.
after analysis of first image, used button go main activity , proceed second activity again choose second image. user chooses second image, android give me out of memory error. tried keeping track of available memory. strange thing right before second image chosen, there more memory available before first image chosen. how should go solving this? thanks!
ps code runs out of memory @ bitmapfactory.decodefile(picturepath);
if running on pre 3.0 hardware, memory value see not include memory used bitmaps, that's possible reason behavior described.
as rule of thumb, should check dimension of image app retrieve dynamically (either user selection or net), , scale size makes sense app. example, gallery app should rescale picture phone takes dimension of screen. below code sample decoding scaled bitmap:
private bitmap decodefile(file f, int width_tmp, int height_tmp, int maxsize) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; inputstream in = new fileinputstream(f); bitmapfactory.decodestream(in, null, o); try { in.close(); } catch (ioexception e1) { } // find correct scale value. should power of 2. int scale = 1; while (maxsize > 0) { if (width_tmp / 2 < maxsize || height_tmp / 2 < maxsize) { break; } width_tmp /= 2; height_tmp /= 2; scale++; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; in = new fileinputstream(f); bitmap bm = bitmapfactory.decodestream(in, null, o2); try { in.close(); } catch (ioexception e1) { } return bm; } catch (filenotfoundexception e) { e.printstacktrace(); } return null; }
Comments
Post a Comment