3

在我的 android 应用程序中,我想将图像转换为字节数组并编码为字符串,以便我可以将其保存在数据库中。但是压缩图像后它的大小变得太小了..我想保持原始大小..请帮帮我..

  final Bitmap image=(images.get(position));

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
                image.compress(Bitmap.CompressFormat.PNG, 100, bytes);

                byte[] b = bytes.toByteArray();
                 encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
4

1 回答 1

0

您可以使用以下内容将其存储在数据库中而无需压缩:

Bitmap bitmap; // obtain bitmap object
int size = bitmap.getRowBytes() * bitmap.getHeight(); 
ByteBuffer b = ByteBuffer.allocate(size); 
bitmap.copyPixelsToBuffer(b); 
byte[] bytes = new byte[size];
b.get(bytes, 0, bytes.length);

然后就可以存入bytes数据库了。

于 2013-10-30T12:10:02.907 回答