Bitmap.compress 对于绘制的图像返回 false
我有一些代码,用户在屏幕上绘制一些内容,我想将其作为 PNG 存储在 byte[] 中。但是,compress() 方法返回 false。知道为什么吗?有没有更好的方法来获取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
}
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于我如何创建图像:
当我将其更改为 Bitmap.Config.RGB_565 时,它工作正常。
感谢 Mark Murphy (@commonsware) 在办公时间内提供的建议!
The problem was in how I was creating the image:
When I changed that to
Bitmap.Config.RGB_565
it worked fine.Thanks to Mark Murphy (@commonsware) for the advice during his office hours!