combining two png files in android

I’ve been trying to figure this out for a little while now.

Here’s (essentially) the code I used to make it work.

// Get your images from their files
Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactory.decodeFile("myOtherPNG.png");

// As described by Steve Pomeroy in a previous comment, 
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);

// comboImage is now a composite of the two. 

// To write the file out to the SDCard:
OutputStream os = null;
try {
    os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
    comboImage.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}

EDIT :

there was a typo,
So, I’ve changed

image.compress(CompressFormat.PNG, 50, os)

to

bottomImage.compress(CompressFormat.PNG, 50, os)

Leave a Comment