【问题标题】:Bitmap.compress returns false for drawn imageBitmap.compress 为绘制的图像返回 false
【发布时间】:2011-05-19 19:18:20
【问题描述】:

我有一些代码,用户在屏幕上绘制了一些东西,我想将它作为 PNG 存储在一个字节 [] 中。但是,compress() 方法返回 false。知道为什么吗?有没有更好的方法来获取字节[]?

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
}

提前致谢。

【问题讨论】:

    标签: android canvas bitmap png


    【解决方案1】:

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

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

    当我将其更改为 Bitmap.Config.RGB_565 时,它运行良好。

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

    【讨论】:

    • 我遇到了同样的问题。你能告诉我为什么ALPHA_8 不起作用吗?
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2020-01-18
    • 2018-12-20
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多