views:

26

answers:

0

I'm attempting to overlay one image on an image taken by the device camera and save the result back to external storage. I'm continually running into:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

which I generally understand but in this case I cannot downsample the image from the camera because I want to preserve the original quality. The camera image is 2592x1456. The overlay image is 490x834. It seems that at a minimum I'll have to:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;

public class TestActivity extends Activity {

    private Bitmap overlay;
    private Bitmap base;
    private Bitmap background;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        base = BitmapFactory.decodeResource(getResources(), R.drawable.base);
        overlay = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);             
        background = BitmapFactory.decodeResouce(getResources(), R.drawable.background);
    }

    @Override
    protected void onStart() {
        Rect target = new Rect(0, 0, 490, 834);

        Canvas canvas = new Canvas(base);
        canvas.drawBitmap(overlay, null, target, null);

        super.onStart();
    }

}

And I'd like to do this while a view is presenting some background. I tried to simulate it by loading the three images in question. If I comment out the background then it works. In this case the background is a downsampled version of base...

Am I missing something obvious in terms of accomplishing this?

Is there a way to perform the overlay in a streaming fashion so the memory is not exhausted?