Bitmap.compress returns false for drawing

I have a code in which the user draws something on the screen, and I want to save it as a PNG in byte []. However, the compress () method returns false. Any idea why this is so? Is there a better way to get 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.

+6
source share
1 answer

The problem was how I created the image:

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

When I changed this to Bitmap.Config.RGB_565 , it worked fine.

Thanks to Mark Murphy (@commonsware) for the tips during his work!

+8
source

Source: https://habr.com/ru/post/888579/


All Articles