如何在Android中将字节数组转换为Blob数据类型

时间:2014-06-18 02:44:39

标签: android android-sqlite

我只是想知道,如何将字节数组转换为blob?

我的数据库如下所示:

Photo
id (int)    |   image_key (blob)

这是我的代码:

 //this for getting data from picture
 Bitmap yourImage = extras.getParcelable("data");
 // convert bitmap to byte
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
 //the problem is right here, i want to convert this byte array, to a blob data type
 byte imageInByte[] = stream.toByteArray();

如何将此字节数组转换为我的数据库照片,并将其放在字段image_key(blob)中?因为我尝试过但却出错了。有人可以帮我解决这个错误吗?我需要帮助..

1 个答案:

答案 0 :(得分:0)

我猜你的代码是

SQLiteDatabase.execSQL("INSERT INTO Photo (image_key) VALUES ('" + byteArray + "')");

这不起作用,因为使用byteArray将转换为字符串“B @ 421a0a0”

使用SQLiteDatabase.execSQL(String sql,Object [] bindArgs)代替

SQLiteDatabase.execSQL("INSERT INTO Photo (image_key) VALUES (?)", byteArray);

这称为Prepared statement