Hi, I'm having a problem with Bitmaps that exceed the size of the VM. In my app, the user takes a picture of about 1200x800 with the camera and something is drawn over it after which it is dsplayed on the screen. For this displaying I resize the image to avoid memory problems.
But when the user chooses to save the image, I want to save not the rescaled image, but the one with the original size, and thats where the problems occur.
I need to draw on the image, without destroying the original image, so I now make a copy of the image on which I do the drawing and which I save. But that way I have the large image in memory twice together with the small image for displaying, and I get memory problems.
Below is my code, is there any way to avoid memory problems? (maybe a way to draw on an image without destroying the original image or something, so I won't have to draw on a copy of the image?)
But when the user chooses to save the image, I want to save not the rescaled image, but the one with the original size, and thats where the problems occur.
I need to draw on the image, without destroying the original image, so I now make a copy of the image on which I do the drawing and which I save. But that way I have the large image in memory twice together with the small image for displaying, and I get memory problems.
Below is my code, is there any way to avoid memory problems? (maybe a way to draw on an image without destroying the original image or something, so I won't have to draw on a copy of the image?)
Code:
try {
Bitmap originalImage = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
Context mContext = getApplicationContext();
Resources res = mContext.getResources();
int a = icons[currentIcon-1];
int largeWidth = originalImage.getWidth();
int largeHeight = originalImage.getHeight();
int newWidth = transWidth*imageScale;
int newHeight = transHeight*imageScale;
Bitmap tempOriginalImage = Bitmap.createBitmap(largeWidth, largeHeight, Bitmap.Config.RGB_565);
Canvas saveCanvas = new Canvas(tempOriginalImage);
Paint paint = new Paint();
saveCanvas.drawBitmap(originalImage, 0, 0, paint);
originalImage.recycle();
int newPositionX = positionX*imageScale;
int newPositionY = positionY*imageScale;
Bitmap icon = BitmapFactory.decodeResource(res, a);
Bitmap largeScaledIcon = Bitmap.createScaledBitmap(icon, newWidth, newHeight, false);
icon.recycle();
saveCanvas.drawBitmap(largeScaledIcon, newPositionX, newPositionY, paint);
largeScaledIcon.recycle();
FileOutputStream out = new FileOutputStream(f);
tempOriginalImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
tempOriginalImage.recycle();
out.close();
}catch(Exception e) {
e.printStackTrace();
}