1

I am just wondering, how can I convert from byte array to blob?

My database looks like this:

Photo
id (int)    |   image_key (blob)

This is my 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();

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..

user3747208
  • 21
  • 1
  • 6
  • what is the error? Looks like this might be a duplicate of a duplicate: http://stackoverflow.com/questions/4191136/how-to-store-and-retrieve-a-byte-array-image-data-to-and-from-a-sqlite-databas – IrishGeek82 Jun 18 '14 at 02:57
  • @IrishGeek82 well, the error look like i can't put the the value of imageInByte [B@421a0a0 into blob data type, so the query is INSERT INTO Photo (image_key) VALUES ('[B@421a0a0'), please correct me, if i'm wrong.. – user3747208 Jun 18 '14 at 03:08
  • Are you using something like this? cv.put(CHUNK, buffer); //CHUNK blob type field of your table long rawId=database.insert(TABLE, null, cv); //TABLE table name – IrishGeek82 Jun 18 '14 at 03:15
  • @IrishGeek82 : no, i'm not using that, am i should use that? cause i think if i use that statement, i can't use WHERE clause, and can i ask, what meaning CHUNK_SIZE, it's that length of byteArray? or something else? – user3747208 Jun 18 '14 at 03:20
  • I pulled that from the link I mentioned where they solved this problem already. http://stackoverflow.com/questions/4191136/how-to-store-and-retrieve-a-byte-array-image-data-to-and-from-a-sqlite-databas I am just trying to get an idea of how best to help you. I think the above link will be beneficial to you. – IrishGeek82 Jun 18 '14 at 03:37
  • @IrishGeek82: okay i get it, but can you explain to me, what meaning of CHUNK_SIZE on this line >byte[] buffer=new byte[CHUNK_SIZE]; it'will help for me.. – user3747208 Jun 18 '14 at 03:42
  • They set a value (which now I see they didn't show) such as final int CHUNK_SIZE = 1024; //1024 bytes for each chunk. They used this value to control the size of the buffer array and to test the size of bytes read from the input stream. The used while (size == CHUNK_SIZE){} to loop until they no longer had read CHUNK_SIZE data from the file. I hope that helps, but I do not think that particular piece of code is specifically relevant to you. – IrishGeek82 Jun 18 '14 at 04:03

1 Answers1

0

I guess your code was

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

That won't work because use byteArray will be converted to a String "B@421a0a0"

use SQLiteDatabase.execSQL(String sql, Object[] bindArgs) instead

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

This is called Prepared statement

Charlie
  • 172
  • 1
  • 1
  • 6
  • thank you for your reply, but it's still error, execSQL is only take String right? – user3747208 Jun 19 '14 at 06:09
  • 06-19 14:57:36.695: E/AndroidRuntime(29152): FATAL EXCEPTION: main 06-19 14:57:36.695: E/AndroidRuntime(29152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.search/com.example.frontend.search.Main}: java.lang.IllegalArgumentException: the bind value at index 1 is null – user3747208 Jun 19 '14 at 08:03
  • and i using this code, SQLiteStatement stmt = db.compileStatement("INSERT INTO foto (caption_foto, image) VALUES ('foto1', ?)"); stmt.bindBlob(1, array); stmt.execute();, is anything wrong? – user3747208 Jun 19 '14 at 08:04
  • "the bind value at index 1 is null",is the array null? – Charlie Jun 19 '14 at 10:08
  • public void bindBlob(int index, byte[] value) { if (value == null) { throw new IllegalArgumentException("the bind value at index " + index + " is null"); } bind(index, value); } source code of android.database.sqlite.SQLiteProgram,can you check the array value? – Charlie Jun 19 '14 at 10:37