Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap.compress returns false for drawn image

I have some code where the user draws something on the screen and I want to store it as a PNG in a byte[]. However, the compress() method returns false. Any idea why that is? Is there a better way to get the byte[]?

Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(bm);
c.drawPath(mSignaturePath, mSignaturePaint);
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (bm.compress(Bitmap.CompressFormat.PNG, 100, out)) {
    byte[] result = out.toByteArray(); // Never gets called
}

Thanks in advance.

like image 363
Shawn Lauzon Avatar asked May 19 '11 19:05

Shawn Lauzon


1 Answers

The problem was in how I was creating the image:

Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ALPHA_8);

When I changed that to Bitmap.Config.RGB_565 it worked fine.

Thanks to Mark Murphy (@commonsware) for the advice during his office hours!

like image 100
Shawn Lauzon Avatar answered Oct 15 '22 16:10

Shawn Lauzon