android - OutofMemoryError while trying to construct Bitmap from byte Array -


i fetching byte array blob type while stored in db, works small image when image size more 200kb gives me error of outofmemoryerror. should overcome such error

photo byte array

bytearrayinputstream imagestream = new bytearrayinputstream(photo);             bitmap theimage= bitmapfactory.decodestream(imagestream);             bitmap bitmapscaled = bitmap.createscaledbitmap(theimage, 100,80, true);             drawable drawable = new bitmapdrawable(bitmapscaled);             imgpath.setbackgrounddrawable(drawable);             imgpath.setscaletype(imageview.scaletype.fit_end); 

logcat error

05-06 15:55:38.871: e/androidruntime(2647): fatal exception: main 05-06 15:55:38.871: e/androidruntime(2647): java.lang.outofmemoryerror 05-06 15:55:38.871: e/androidruntime(2647):     @ android.graphics.bitmapfactory.nativedecodestream(native method) 05-06 15:55:38.871: e/androidruntime(2647):     @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:493) 05-06 15:55:38.871: e/androidruntime(2647):     @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:549) 05-06 15:55:38.871: e/androidruntime(2647):     @ com.example.hotelmenu.revisedmainmenu.displaymenu(revisedmainmenu.java:655) 05-06 15:55:38.871: e/androidruntime(2647):     @ com.example.hotelmenu.revisedmainmenu.onclick(revisedmainmenu.java:615) 

the image size not matter. what's matter width , height. infact bitmap instance retains width*height*4 bytes. if getting oom suggest downsample bitmap .

also

 bitmap theimage= bitmapfactory.decodestream(imagestream);  bitmap bitmapscaled = bitmap.createscaledbitmap(theimage, 100,80, true); 

in snippet provide, after bitmapscaled created, theimage in never used. should recycle calling

theimage.recycle(). 

edit. snippet create bitmap 1/4 wider , higher original

 bitmapfactory.options options=new bitmapfactory.options();  options.insamplesize = 4;  bitmap yourselectedimage = bitmapfactory.decodestream(imagestream, null, options ); 

Comments