• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Bitmap out of memory

android88

Newbie
Jan 6, 2010
17
0
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?)

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();
}
 
It's hard to solve your problem without knowing e.g. a stacktrace that gives the exact location where the OutOfMemoryError occurs, but here are some remarks:
- originalImage.recycle() renders the original image useless. So if you use that method, then there is basically no point in creating a copy, but you could as well use the original image.
- In order to save memory you could try to first save a compressed version of the original bitmap, using a lossless format like PNG, to e.g. a ByteArrayOutputStream, then work on the original image and finally restore the content of the original image from the previously saved data. This avoids the memory allocation for a new, large bitmap. OTOH it takes some time for the PNG compression. Just a thought.
 
Upvote 0
It's hard to solve your problem without knowing e.g. a stacktrace that gives the exact location where the OutOfMemoryError occurs, but here are some remarks:
- originalImage.recycle() renders the original image useless. So if you use that method, then there is basically no point in creating a copy, but you could as well use the original image.
- In order to save memory you could try to first save a compressed version of the original bitmap, using a lossless format like PNG, to e.g. a ByteArrayOutputStream, then work on the original image and finally restore the content of the original image from the previously saved data. This avoids the memory allocation for a new, large bitmap. OTOH it takes some time for the PNG compression. Just a thought.

Thanks for the remarks :)

You're right about the recycle of originalImage, i'm not sure what my reasoning was there :S I think that's something I did before I was using the Mediastore and I never changed it. So I can work directly on originalImage and then the image in the mediastore will not change. That should free up enough memory I think :)
THANKS
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones