android - how to correct this error: java.lang.OutOfMemoryError -


this code:

public void onpicturetaken(byte[] data, camera camera) {     bitmap foto = bitmapfactory.decodebytearray(data, 0, data.length);     wid = foto.getwidth();     hgt = foto.getheight();      bitmap newimage = bitmap.createbitmap(wid, hgt, bitmap.config.argb_4444);     canvas canvas = new canvas(newimage);     canvas.drawbitmap(foto, 0f, 0f, null);     if (newimage.getwidth() > newimage.getheight()) {         matrix matrix = new matrix();         matrix.postrotate(90);         newimage.createbitmap(newimage, 0, 0, wid, hgt, matrix, true);      } } 

this error:

fatal exception: main java.lang.outofmemoryerror @ android.graphics.bitmap.nativecreate(native method) @ android.graphics.bitmap.createbitmap(bitmap.java:689) @ android.graphics.bitmap.createbitmap(bitmap.java:666) @ android.graphics.bitmap.createbitmap(bitmap.java:633) @ com.supratecnologia.activity.camera_activity.onpicturetaken(camera_activity.java:189) @ android.hardware.camera$eventhandler.handlemessage(camera.java:768) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method) 

ost of times happens due large bitmap cross limit of vm heap, need scale down image or lower quality of image.

to scale image use bitmapfactory.options.

bitmap bm = null; bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 5; assetfiledescriptor filedescriptor =null; try {     filedescriptor = this.getcontentresolver().openassetfiledescriptor(selectedimage,”r”); } catch (filenotfoundexception e) {     e.printstacktrace(); } finally{     try {         bm = bitmapfactory.decodefiledescriptor(filedescriptor.getfiledescriptor(), null, options);         filedescriptor.close();     } catch (ioexception e) {         e.printstacktrace();     } }  

or use bitmap.compress() lower quality of image.

and can follow conversation avoid memory error while loading images in bitmap.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -