android - Bitmap.compress 为绘制的图像返回 false

标签 android canvas bitmap png

我有一些代码,用户可以在屏幕上绘制一些东西,我想将它作为 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
}

提前致谢。

最佳答案

问题在于我如何创建图像:

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

当我将其更改为 Bitmap.Config.RGB_565 时,它工作正常。

感谢 Mark Murphy (@commonsware) 在办公时间提供的建议!

关于android - Bitmap.compress 为绘制的图像返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063764/

相关文章:

javascript - 厄运的碰撞循环

go - 在 Golang 中将 GDI32.dll 位图保存到磁盘

android - Google Play 商店应用提交规则

Java 和 LibGdx - 运行时出现 fatal error

javascript - 受 jQuery 滑动切换影响的 Canvas 大小

javascript - 在 CANVAS 上绘制图像数组

android - 如何从 Android 中的 Assets 加载图像列表?

android - 无法在手机内存中保存位图

android - 奇怪的 AsyncTask 行为

android - 解决错误: all support libraries must use the exact same version的正确方法