Android 不同质量的压缩不起作用

Android compressing with different quality doesn't work

当我将质量从 100 更改为 0(任何值)时,压缩前和压缩后的位图大小始终具有相同的大小。为什么?以及如何真正压缩它?

public static void checkSize(Bitmap tempBitmap, Context context) {
    Log.d("IMAGES_", "SIZE before = " + tempBitmap.getByteCount());
    Bitmap newBit = compress(tempBitmap, Bitmap.CompressFormat.PNG, 3);
    Log.d("IMAGES_", "SIZE after = " + newBit.getByteCount());
}

public static Bitmap compress(Bitmap src, Bitmap.CompressFormat format,
                            int quality) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    src.compress(format, quality, os);
    byte[] array = os.toByteArray();
    return BitmapFactory.decodeByteArray(array, 0, array.length);
}

结果:

你测错了。压缩大小由 array.length 给出。然后您将其解码回其完整大小和像素编码。如果你想拥有更小的内存占用,你需要:

  1. 调整图像大小。
  2. 使用不同的像素格式解码。
  3. 以上所有。
  1. 质量参数不适用于无损的PNG格式,需要改为JPG格式才能生效

  2. 即使您将其更改为 JPG,前后位图仍然具有相同的尺寸,但质量不同。我认为您真正需要的是调用 Bitmap.createScaledBitmap() 来获取较小的位图然后存储它。