I need to create and save single color PNG images (bitmap filled with a single color).
I'm creating the bitmap:
public static Bitmap createColorSwatchBitmap(int width, int height, int color) {
final Bitmap colorBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
colorBitmap.eraseColor(color);
return colorBitmap;
}
and saving it to a file on the device storage:
stream = new FileOutputStream(filePath);
success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
If I create a 1200x1200 bitmap, the memory consumption is 5,760,000 bytes (5.76 MB), as reported by bitmap.getAllocationByteCount(). However the PNG file size is only 8,493 bytes.
It seems so overkill to allocate almost 6 MB of memory for a file that will only have 8 KB.
It there a better way?