7

This is my 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 is my error:

FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:689)
at android.graphics.Bitmap.createBitmap(Bitmap.java:666)
at android.graphics.Bitmap.createBitmap(Bitmap.java:633)
at com.supratecnologia.activity.Camera_Activity.onPictureTaken(Camera_Activity.java:189)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:768)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
JJD
52.7k63 gold badges218 silver badges352 bronze badges
asked Aug 19, 2013 at 19:26
2
  • The error is pretty clear, you're using too much memory I recommend you to check the memory usage of your application and track exactly when this error happens. Commented Aug 19, 2013 at 19:35
  • stackoverflow.com/questions/17990086/… Commented Aug 30, 2013 at 9:24

2 Answers 2

10

ost of times this happens due to large bitmap cross the limit of VM heap, so you need to scale down your image or to lower the quality of you 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() to lower quality of image.

And you can also follow this conversation to avoid memory error while loading images in Bitmap.

answered Aug 19, 2013 at 19:52
Sign up to request clarification or add additional context in comments.

Comments

0

If you are using an emulator you might have a value for VM Heap too low for that emulator instance. Check the Advanced Option in the emulator and try increasing the value for VM Heap. It happened to me that in my phone was working but not in the emulator for the low VM Heap value. Anyway, if you don't have lots of images probably they have a resolution too high.

answered Dec 16, 2015 at 10:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.