从SQLite检索图像时,BitmapFactory.decodeStream(inputStream)返回null

时间:2016-10-27 14:33:00

标签: android arrays image sqlite blob

我的SQLite表中有一个BLOB类型的列,并且将一个图像的byte []从drawable保存到它中。现在,在检索时,我正在使用游标

访问blob
cursor.getBlob(cursor.getColumnIndex(SQLiteHelper.col_product_image))

然后使用以下代码显示从上面的代码中检索到的byte []

ByteArrayInputStream inputStream = new ByteArrayInputStream(bb);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            iv.setImageBitmap(bitmap);

但图像视图中没有显示任何内容,因为BitmapFactory.decodeStream(inputStream); 是空的。

bb的值是[91,66,64,52,51,54,102,51,53,51,48,0]

帮助!

1 个答案:

答案 0 :(得分:0)

您的二进制数据存储不正确。

字节

91, 66, 64, 52, 51, 54, 102, 51, 53, 51, 48, 0

映射到字符串

[B@436f3530

看起来像toString()的{​​{1}}输出,而不是字节数组本身。

此外,Android sqlite不适用于存储大型(2MB或更多)二进制数据,如图像。最好将图像保存到文件系统,然后将路径存储在数据库中。