Android - Saving photo taken from Camera , Out of memory

Anukool

I have a use case where the user takes a picture and the picture has to be saved on sd card .

I have set the orientation of the camera as portrait .

While saving the photo on to SD card i am getting out of memory .

Here is the log -

java.lang.RuntimeException: An error occured while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:300)
  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.OutOfMemoryError
  at android.graphics.Bitmap.nativeCreate(Native Method)
  at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
  at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
  at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
  at com.philips.cl.di.haircare.util.AppUtility.changeOrientationAndwriteDataToFile(AppUtility.java:382)
  at com.philips.cl.di.haircare.mirror.MirrorCameraViewFragment$SaveBitMap.doInBackground(MirrorCameraViewFragment.java:389)
  at com.philips.cl.di.haircare.mirror.MirrorCameraViewFragment$SaveBitMap.doInBackground(MirrorCameraViewFragment.java:369)
  at android.os.AsyncTask$2.call(AsyncTask.java:288)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  ... 4 more

Here is the function that i am using to save -

public static boolean changeOrientationAndwriteDataToFile(Context context,
        final File pictureFile, final byte[] data, final int camId)
        throws Exception {

    FileOutputStream fos = null;
    Bitmap realImage = null;
    try {

        fos = new FileOutputStream(pictureFile);
        realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(camId, info);
        Matrix matrix = new Matrix();

        int rotation = info.orientation;
        int camfacing = info.facing;

        if (camfacing == 1) {
            matrix.setScale(1, -1);
        }
        if (rotation != 0) {
            matrix.postRotate(rotation);

        } else {
            matrix.postRotate(-90);
        }
        realImage = Bitmap.createBitmap(realImage, 0, 0,
                realImage.getWidth(), realImage.getHeight(), matrix, false);
        MediaScannerConnection.scanFile(context,
                new String[] { pictureFile.getPath() },
                new String[] { "image/jpeg" }, null);

        // 100 means maximum quality
        return realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    } catch (final Exception e) {
        throw e;

    } finally {
        if (realImage != null) {
            realImage.recycle();
        }
        if (fos != null) {
            fos.close();
        }
    }

}

Line number - 382 is going out of memory .

realImage = Bitmap.createBitmap(realImage, 0, 0,
                realImage.getWidth(), realImage.getHeight(), matrix, false);

Please help on how to resolve this .

Thanks.

Anukool

I solved it using BitmapFactory Options Here is the updated method -

public static boolean changeOrientationAndwriteDataToFile(Context context,
        final File pictureFile, final byte[] data, final int camId, int reqWidth , int reqHeight )
        throws Exception {

    FileOutputStream fos = null;
    Bitmap realImage = null;
    Bitmap transformedImage = null ;
    try {

        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(camId, info);
        int rotation = info.orientation;
        int camfacing = info.facing;

        // Out of memory issue fixx -with the sample size concept

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true ;

        BitmapFactory.decodeByteArray(data, 0, data.length, options);

        options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);

        System.out.println("Sample Size : " + options.inSampleSize);

        options.inPreferredConfig = Bitmap.Config.RGB_565;

        options.inJustDecodeBounds = false;

        realImage= BitmapFactory.decodeByteArray(data, 0, data.length, options);

        Matrix matrix = new Matrix();

        if (camfacing == 1) {
            matrix.setScale(1, -1);
        }
        if (rotation != 0) {
            matrix.postRotate(rotation);

        } else {
            matrix.postRotate(-90);
        }
        transformedImage = Bitmap.createBitmap(realImage, 0, 0,
                realImage.getWidth(), realImage.getHeight(), matrix, false);
        MediaScannerConnection.scanFile(context,
                new String[] { pictureFile.getPath() },
                new String[] { "image/jpeg" }, null);

        fos = new FileOutputStream(pictureFile);
        // 100 means maximum quality
        return transformedImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    } catch (final Exception e) {
        throw e;

    } finally {
        if (realImage != null) {
            realImage.recycle();
        }
        if(transformedImage !=null)
        {
            transformedImage.recycle();
        }
        if (fos != null) {
            fos.close();
        }
    }

}

Here - reqWidth and reqHeight is the screen width and screen height as i have to display the bitmap on the full screen .

Thanks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving photo from camera to a directory in android app fails

From Dev

Saving image taken from camera into INTERNAL storage

From Dev

Problems with saving photo from Camera for Oreo

From Dev

Android Camera: Get Picture Uri after the photo is taken

From Dev

Android: Can't get the size of photo taken by camera acitivity

From Dev

Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

From Dev

ImageView does not display image taken from phone camera or photo gallery

From Dev

taken from android camera image original resolution

From Dev

Taking a photo and saving it to the Camera Roll

From Dev

How to detect if photo was taken (camera intent)

From Dev

Photo taken through camera intent is losing on quality

From Dev

I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

From Dev

How to find out when was a photo taken?

From Dev

Choose photo from camera

From Dev

Delete picture taken from camera

From Dev

Android : Take a photo from with in my layout without starting a camera intent

From Dev

Custom camera shows dark picture preview when photo taken on Nexus

From Dev

Android: Taken photo is bigger than the preview

From Dev

Getting location data for a taken photo in Android

From Dev

Android Cordova - How to draw photo taken on canvas?

From Dev

VBScript to extract "Date Taken" from a photo

From Dev

saving photo on online server android

From Dev

Android camera and photo picker intent

From Dev

Saving a photo from mobile to GDrive

From Dev

Find picture taken out of memory issue

From Dev

Titanium: Saving file locally from Android's Camera/Gallery image

From Dev

Saving taken picture on Android is always 0 byte

From Dev

File not exist android image taken by camera

From Dev

Android adding image to picture taken with camera

Related Related

  1. 1

    Saving photo from camera to a directory in android app fails

  2. 2

    Saving image taken from camera into INTERNAL storage

  3. 3

    Problems with saving photo from Camera for Oreo

  4. 4

    Android Camera: Get Picture Uri after the photo is taken

  5. 5

    Android: Can't get the size of photo taken by camera acitivity

  6. 6

    Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

  7. 7

    ImageView does not display image taken from phone camera or photo gallery

  8. 8

    taken from android camera image original resolution

  9. 9

    Taking a photo and saving it to the Camera Roll

  10. 10

    How to detect if photo was taken (camera intent)

  11. 11

    Photo taken through camera intent is losing on quality

  12. 12

    I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

  13. 13

    How to find out when was a photo taken?

  14. 14

    Choose photo from camera

  15. 15

    Delete picture taken from camera

  16. 16

    Android : Take a photo from with in my layout without starting a camera intent

  17. 17

    Custom camera shows dark picture preview when photo taken on Nexus

  18. 18

    Android: Taken photo is bigger than the preview

  19. 19

    Getting location data for a taken photo in Android

  20. 20

    Android Cordova - How to draw photo taken on canvas?

  21. 21

    VBScript to extract "Date Taken" from a photo

  22. 22

    saving photo on online server android

  23. 23

    Android camera and photo picker intent

  24. 24

    Saving a photo from mobile to GDrive

  25. 25

    Find picture taken out of memory issue

  26. 26

    Titanium: Saving file locally from Android's Camera/Gallery image

  27. 27

    Saving taken picture on Android is always 0 byte

  28. 28

    File not exist android image taken by camera

  29. 29

    Android adding image to picture taken with camera

HotTag

Archive