我将位图转换为字节数组,也将字节数组转换为位图,但当我要在ImageView中显示转换后的字节数组时,它会显示带有黑色角落的图像,而不是以PNG格式显示。我想以PNG格式显示图像,该怎么办?
以下是将位图转换为字节数组和将字节数组转换为位图的代码:
将位图转换为PNG压缩格式的字节数组:
public byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream stream = null;
try {
stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e(Helper.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
将字节数组转换为位图,代码如下:
BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);