Android – How to change the background color of a saved transparent bitmap

androidbitmaptransparency

I am opening a png image into a Bitmap, making some modifications to it and then saving it to disk as a jpg. In the case where the png has some transparent areas, they are saved as black. Is there a way to change this default behavior so the image is saved with a different color background such as white?

Thanks

Best Answer

You could draw it to a new bitmap, e.g.

   Bitmap newBitmap = Bitmap.createBitmap(image.getWidth(), 
    image.getHeight(), image.getConfig());
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(image, 0F, 0F, null);

then save new bitmap instead