I am well aware of the OutOfMemoryError hell that working with bitmaps can land image-heavy apps in. Normally, I always work around this by adaptively scaling the images, using caching, etc.
However, currently I work on an app where one use case is the ability to take an image and have it automatically watermarked by (i.e. merged with) another, smaller image. The specification unambiguously states that the branded image must be of the same size and quality as the original one, so I am not able to scale.
I have already found this impossible to implement: attempting to create a bitmap with the same dimensions as the original image always throws OOME on the Galaxy S3 I use for testing, even if android:largeHeap="true" is set.
Is there any possibility at all to get around this?
The relevant code is:
private Bitmap mergeImages(Bitmap firstImage, Bitmap secondImage) {
Bitmap.Config config = (firstImage.getConfig() != null) ? firstImage.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap resultImage = Bitmap.createBitmap(
Math.max(firstImage.getWidth(), secondImage.getWidth()),
Math.max(firstImage.getHeight(), secondImage.getHeight()),
config);
Canvas mergeCanvas = new Canvas(resultImage);
Paint paint = new Paint();
mergeCanvas.drawBitmap(firstImage, 0, 0, paint);
mergeCanvas.drawBitmap(secondImage, 0, 0, paint);
return resultImage;
}