I am trying to make an app where I need to store and retrieve a image in and from a SQLite Database. I am getting the program to store the image as a byte array using BLOB and I am also able to retrieve the byte array, but while decoding the array into a bitmap, BitmapFactory returns null. Please help. Here is the code I am using to decode the array.
Cursor c=db.rawQuery("SELECT * FROM student WHERE name='"+"1"+"'", null);
if(c.moveToFirst())
{
byte[] outImage=c.getBlob(1);
Log.d("The out image is", String.valueOf(outImage));
Bitmap bitmap = BitmapFactory.decodeByteArray(outImage , 0, outImage .length);
iv.setImageBitmap(bitmap);
}
This is how I am inserting the byte[] in database:
if(requestCode==CAMERA_REQUEST && resultCode==RESULT_OK){
Bitmap yourImage = (Bitmap) data.getExtras().get("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.execSQL("INSERT INTO student VALUES('"+"1"+"','"+imageInByte+"');");
Log.d("Success: ", "Image Saved");
}
The logcat reads something like this
12-24 23:39:49.777: E/output before conversion(22523): [B@13fd7069
12-24 23:39:49.778: D/Insert:(22523): Inserting ..
12-24 23:39:49.840: D/Success:(22523): Image Saved
12-24 23:39:51.579: D/The out image is(22523): [B@155f0aee
12-24 23:39:51.580: D/skia(22523): --- SkImageDecoder::Factory returned null