且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将字节数组中的图像文件数据转换为位图?

更新时间:2023-11-15 20:03:28

只需尝试以下操作:

Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();

如果 bitmapdata 是字节数组,则按以下方式获取 Bitmap :

If bitmapdata is the byte array then getting Bitmap is done like this:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

返回已解码的 Bitmap ,如果图像无法解码,则返回 null .

Returns the decoded Bitmap, or null if the image could not be decoded.