0

Getting java.lang.OutOfMemory Exception -- Bitmap

I know inSampleSize will help me to resolve my issue, but little bit confuse how it has to be use in my code.

Exception line:-

bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),

showPicture() method:

private void showPicture(){
        if(pic!=null) tempBM = BitmapFactory.decodeFile(pic);
        Bitmap bMapRotate;
        Matrix matrix = new Matrix();
        if(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
            matrix.postConcat(matrixMirrorY);
            bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),
            tempBM.getHeight(), matrix, true);

        }

        if(Utility.isTablet(this)) {
             int orient = getResources().getConfiguration().orientation;

            if(orient==1){
                matrix.postRotate(90);
                bMapRotate = Bitmap.createBitmap(tempBM, 0,0,tempBM.getWidth(),
                tempBM.getHeight(), matrix ,true);
                tempBM = bMapRotate;
            }

        }else{
            //Get Orientation:
            int orientation;

            if(tempBM.getHeight() < tempBM.getWidth()){
                orientation = 90;
            } else {
                orientation = 0;
            }
            if (orientation != 0) {
                matrix.postRotate(orientation);
                bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),
                        tempBM.getHeight(), matrix, true);

            } else
                bMapRotate = Bitmap.createScaledBitmap(tempBM, tempBM.getWidth(),
                        tempBM.getHeight(), true);

            tempBM = bMapRotate;
        }

Log:-

01-29 10:16:04.625: E/AndroidRuntime(20234): java.lang.OutOfMemoryError
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.nativeCreate(Native Method)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity.showPicture(CameraAPIActivity.java:685)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity.access$1(CameraAPIActivity.java:650)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity$1.onPictureTaken(CameraAPIActivity.java:539)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:789)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.os.Looper.loop(Looper.java:137)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.app.ActivityThread.main(ActivityThread.java:4921)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at java.lang.reflect.Method.invoke(Method.java:511)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at dalvik.system.NativeStart.main(Native Method)
8
  • 3
    please provide some code , and the full stack trace Commented Jan 29, 2014 at 5:00
  • @Amrola please check now Commented Jan 29, 2014 at 5:04
  • please tell me what code is at line 685 in CameraAPIActivity.java Commented Jan 29, 2014 at 5:08
  • my guess is: Bitmap.createBitmap(tempBM, 0,0,tempBM.getWidth(), Commented Jan 29, 2014 at 5:09
  • @Amrola yeah gian1200 is right i highlighted above Commented Jan 29, 2014 at 5:12

3 Answers 3

1

Bitmaps take up lots of memory, especially big ones. You most likely need to scale the Bitmap down before loading it into memory. This tutorial is a good example of how to go about that.

Sign up to request clarification or add additional context in comments.

Comments

1

I think it's because your tempBM is too large to store in memory. You can use BitmapFactory.Options when decoding your resource file to make your bitmap smaller. See this

2 Comments

@suitanshi can you show me by making changes in my existing code
@AbrahimNeil. There's already a training class about using inSampleSize, refer to this. You can simply copy some code to your project. It's easy to understand.
0

First stop your application from crashing at run time by catching the OutOfMemory error at place which you think can generate this error as below:

try {

...

}
catch(OutOfMemoryError error)  {
    //decide what to do when there is not more memory available
}

(OR)

Runtime.getRuntime().gc();

Calling Garbage Collector is a good Idea.

1 Comment

we should not rely on gc. The ideal solution should be scaling tempBM down

Your Answer

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.