Android : CreateScaledBitmap causing Grow heap

on Sunday, August 31, 2014


new to Android there is something i really do to understand with bitmap.


I use a custom camera which i open on a Handler thread to take photo on my jpeg callback i want to store my picture in cache after scaling down the picture so the pictures fits in a 1000 x 1000 size keeping its ratio.


To do so i take the picture from the jpeg callback using :



private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, final Camera camera) {

Bitmap realImage;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inPurgeable=true;
options.inInputShareable=true;
realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);


So far so good, i can take as many pictures as i want, no memory problem...


But when i want to write the picture to cache i need to scaled it down using :



private class addThumbnailASYNC extends AsyncTask<Bitmap, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Bitmap... bitmap) {

Bitmap image = bitmap[0];
int width = image.getWidth();
int height = image.getHeight();

float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 0) {
width = MAX_SIZE_IMAGE;
height = (int) (width / bitmapRatio);
} else {
height = MAX_SIZE_IMAGE;
width = (int) (height * bitmapRatio);
}

image = Bitmap.createScaledBitmap(image, width, height, true);


the problem is when i use image = Bitmap.createScaledBitmap I get a Grow heap which i think will lead to an out of memory if i take many picture.


There are 3 things that i do understand : - When i use options.inSampleSize the picture should be scaled by 4 so why createScaledBitmap causes a grow heap ? - If i do not use createScaledBitmap and that i write the image to cache i never get the grow heap why ? The inSampleSize should gave me more heavy file than the one i create using scaledBitmap ? - I need to avoid memory heap, but how to avoid grow heap because i have no choice using createdScaledBitmap and my picture will be always taken by the camera, i feel stuck.


Thank you for any help, any clues or more ...


0 comments:

Post a Comment