2014-06-18 36 views
1

我只是想知道,我该如何将字节数组转换为blob?如何将字节数组转换为Android中的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) ?因为我尝试过但有错误。有人能帮我解决这个错误吗?请我需要帮助..

+0

我错误?看起来像这可能是重复的副本:http://stackoverflow.com/questions/4191136/how-to-store-and-retrieve-a-byte-array-image-data-to-and-from-a -sqlite-databas – IrishGeek82

+0

@ IrishGeek82好吧,错误看起来像我不能把imageInByte [B @ 421a0a0的值放到blob数据类型中,所以查询是INSERT INTO Photo(image_key)VALUES('[B @ 421a0a0' ),请纠正我,如果我错了。 – user3747208

+0

您是否在使用类似的东西? cv.put(CHUNK,buffer); // table CHUNK blob type字段 long rawId = database.insert(TABLE,null,cv); // TABLE table name – IrishGeek82

回答

0

我猜你的代码是

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

这是行不通的,因为使用的ByteArray将被转换为字符串“B @ 421a0a0”

使用SQLiteDatabase.execSQL( SQL字符串,对象[] bindArgs),而不是

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

这就是所谓的Prepared statement

+0

谢谢你的回复,但是还是错误的,execSQL只是拿String而已? – user3747208

+0

请添加您的logcat错误和execSQL代码 – Charlie

+0

06-19 14:57:36.695:E/AndroidRuntime(29152):致命例外:主 06-19 14:57:36.695:E/AndroidRuntime(29152):java.lang .RuntimeException:无法启动活动ComponentInfo {com.example.search/com.example.frontend.search.Main}:java.lang.IllegalArgumentException:索引1处的绑定值为空 – user3747208

相关问题