如何在不降低质量的情况下将位图转换为字节数组并返回?

时间:2019-05-27 11:43:33

标签: java android image bitmap

当我将图片转换为字节并返回时,质量会大大降低。如何在不降低质量的情况下做到这一点?

我需要将照片保存在数据库中或将其发送到服务器。我可以考虑使用其他方法来做到这一点。

public static byte[] fromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}
public static Bitmap toBitmap(byte[] bytes) {
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}

2 个答案:

答案 0 :(得分:1)

这不是直接将图片保存在数据库中的好方法。最好的方法是将图片上传到目录中的服务器,并将图片路径保存在数据库中。 Here您可以找到完整的教程,将图片上传到服务器上并将其路径保存在MYSQL数据库中。

答案 1 :(得分:0)

  

如何在不降低质量的情况下做到这一点?

使用无损图像格式,例如PNG。