如何转换字节数组BLOB数据类型在Android中 [英] How to Convert Byte Array to Blob data type in Android
本文介绍了如何转换字节数组BLOB数据类型在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道,我怎样才能从字节数组转换为BLOB?
I am just wondering, how can I convert from byte array to blob?
我的数据库看起来是这样的:
My database looks like this:
Photo
id (int) | image_key (blob)
这是我的code:
//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)?因为我试过,但得到了一个错误。任何人可以帮助我这个错误?请我需要帮助。
How can I convert this byte array into my database photo, and put it in field image_key (blob)? Because I tried but got an error. Can anybody help me about this error? Please I need help..
推荐答案
我猜你的code是
SQLiteDatabase.execSQL("INSERT INTO Photo (image_key) VALUES ('" + byteArray + "')");
这是行不通的,因为使用的字节数组将被转换成字符串B @ 421a0a0
That won't work because use byteArray will be converted to a String "B@421a0a0"
使用SQLiteDatabase.execSQL(SQL字符串,对象[] bindArgs),而不是
use SQLiteDatabase.execSQL(String sql, Object[] bindArgs) instead
SQLiteDatabase.execSQL("INSERT INTO Photo (image_key) VALUES (?)", byteArray);
这就是所谓 prepared声明
这篇关于如何转换字节数组BLOB数据类型在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文