I am using following code: Note that i HAVE TO SEND SQL Query so using this procedure.
currentReceipt.image is a byte[]
String updateQuery = "INSERT INTO MAReceipts(userId, transactionId, transactionType, receiptIndex, referenceNo, image, smallThumb, comments, createdOn, updatedOn) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
java.util.Date today = new java.util.Date();
long t = today.getTime();
java.sql.Date now = new java.sql.Date(t);
for(int i = 0; i < receipts.size(); i++)
{
try{
Receipt currentReceipt = receipts.get(i);
String[] valVars = {
stringToDB(transaction.userId),
integerToDB(transaction.transactionId),
integerToDB(transaction.transactionType.getValue()),
integerToDB(i),
stringToDB(currentReceipt.referenceNo),
(currentReceipt.image != null ? imageToDB(currentReceipt.image): "null"),
(currentReceipt.smallThumb != null ? imageToDB(currentReceipt.smallThumb): "null"), // NEED TO CHANGE THIS TO SMALL THUMB
stringToDB(currentReceipt.comments),
dateToDB(now),
dateToDB(now)
};
mDb.execSQL(updateQuery, valVars);
}catch (Exception e){
Log.e("Error in transaction", e.toString());
return false;
}
}
public String imageToDB (byte[] image)
{
String convertedImage = image.toString();
return convertedImage;
}
return convertedImage shows a value of [B@43eb4218 or similar to that.
Now this data is saved in database. Please tell me is the image is correctly saving in database and can i retrieve it? If not then any preferable way , do tell me.
Best Regards
Storing an image in SQLite is not a good idea. You should save the image on SD card and store its path in database.
Anyhow, if you still want to store the image in database, then you need to do the following:
Base64encoded string*Reversing the process will give the image.
Another solution is to convert your column smallThumb type to
BLOBand store the bytes directly. But for this you need to modify your table.In your code, change the method imageToDB as below: