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.