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)
Sun
  • 6,768
  • 25
  • 76
  • 131

3 Answers3

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.

NasaGeek
  • 2,138
  • 1
  • 15
  • 19
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

suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • @suitanshi can you show me by making changes in my existing code – Sun Jan 29 '14 at 05:13
  • @AbrahimNeil. There's already a training class about using `inSampleSize`, refer to [this](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html). You can simply copy some code to your project. It's easy to understand. – suitianshi Jan 29 '14 at 05:16
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.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138