1

我正在尝试将图像保存在 sqlite 数据库中,然后再检索它。我以 byte[] 的形式存储了它。BUt 位图没有形成!

存储图像:(blob 类型)

Drawable myDrawable = getResources().getDrawable(arr[i]);
myLogo = ((BitmapDrawable) myDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();

我的检索代码:

byte[] Image;
Image = c.getBlob(c.getColumnIndex("img_str"));
BitmapFactory.Options options = new BitmapFactory.Options();
decodedByte = BitmapFactory.decodeByteArray(Image, 0,Image.length, options);
arr_img.add(decodedByte);

System.out.println("Image = " + Image);
System.out.println("decodedByte = " + decodedByte);

日志猫:

07-22 07:15:55.482: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.482: I/System.out(19319): Image = [B@4057ea48
07-22 07:15:55.482: I/System.out(19319): decodedByte = null
07-22 07:15:55.521: D/dalvikvm(19319): GC_EXPLICIT freed 114K, 50% free 2871K/5639K,external 5406K/5783K, paused 43ms
07-22 07:15:55.541: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.541: I/System.out(19319): Image = [B@40577aa0
07-22 07:15:55.541: I/System.out(19319): decodedByte = null

请帮忙,因为我不知道为什么这不起作用。谢谢

4

2 回答 2

1

您的图像字节非常小。你确定这是全图吗?根据您的输出,图像只有 22 个字节,而且很小。我之所以这么说是因为 API 声明:

Returns The decoded bitmap, or null if the image could not be decode.

我相信它image没有它应该的那么大。

尝试使用此代码再次将您的图像存储在数据库中并尝试再次检索:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();
于 2013-07-22T08:12:01.417 回答
0

实际上问题不在于转换,而是我存储它的方式..

我使用准备好的语句来插入值.. bindBlob() 完成了这项工作

String sql = "INSERT INTO demoImage (img_name,img_str) VALUES(?,?)";

    SQLiteStatement insertStmt = dbsqlite.compileStatement(sql);
    insertStmt.clearBindings();
    insertStmt.bindString(1, name);
    insertStmt.bindBlob(2, bal);
    insertStmt.executeInsert();
于 2013-08-02T06:19:26.667 回答