Saturday, 15 March 2014

Avoid OutOfMemoryError for Bitmap.createScaledBitmap in Android -



Avoid OutOfMemoryError for Bitmap.createScaledBitmap in Android -

i using next code enlarge image.

bmp=new bitmapfactory().decodefile(util.getpathfromuri(tempfile)); bitmap scaledbitmap=bitmap.createscaledbitmap(bmp, newwidth, newheight, true); //line 2 bytearrayoutputstream bos=new bytearrayoutputstream(); scaledbitmap.compress(compressformat.jpeg, 100, bos); bmp.recycle(); bmp=null; outputstream out; out = new fileoutputstream(util.gettempfilename()); bos.writeto(out); bos.flush();

but outofmemoryerror occurs @ line 2 , app crashes, tried enclosing code within try-catch still app crashes exceptions caught try-catch, createscaledbitmap() functions throws illegalargumentexception. since, don't want display image hence don't need ot scale down(as saw in other questions in sof). so, how can pre-detect outofmemoryerror occur if utilize (newwidth, newheight). there way calculate bytes required bitmap of (newwidth, newheight) in memory , max available memory can allocated bitmap?

please help!

bitmapfactory.options options = new bitmapfactory.options(); options.inpreferredconfig = config.rgb_565; options.inpurgeable = true; options.insamplesize = 1; options.inscaled = false; options.indensity = displaymetrics.density_default; options.intargetdensity = displaymetrics.density_default; options.inscreendensity = displaymetrics.density_default; new bitmapfactory().decodefile(util.getpathfromuri(tempfile),options);

android bitmap out-of-memory

No comments:

Post a Comment