如何将字节数组中的图像文件数据转换为位图? [英] How to convert image file data in a byte array to a Bitmap?

查看:58
本文介绍了如何将字节数组中的图像文件数据转换为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像存储在 SQLite数据库中.我尝试使用 BLOB String 存储它,在两种情况下,它都存储图像并可以检索它,但是当我使用以下命令将其转换为 Bitmap BitmapFactory.decodeByteArray(...)它返回null.

I want to store image in SQLite DataBase. I tried to store it using BLOB and String, in both cases it store the image and can retrieve it but when i convert it to Bitmap using BitmapFactory.decodeByteArray(...) it return null.

我已经使用了这段代码,但是它返回null

I have used this code, but it returns null

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

推荐答案

只需尝试以下操作:

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.

这篇关于如何将字节数组中的图像文件数据转换为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆