0

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
  • 1
    I don't think you can get the byte array from blob like that: http://stackoverflow.com/questions/6662432/easiest-way-to-convert-a-blob-into-a-byte-array – JASON G PETERSON Dec 24 '14 at 18:23
  • 1
    "I am getting the program to store the image as a byte array using BLOB" - Don't. Let the file system manage things like that. Store your image as a regular file, and keep only the file name (and path) in your database. – Mike M. Dec 24 '14 at 18:25
  • @MikeM. that thought occurred to me as well, but in my case this was the best way to go about, so I stuck with SQLite. Thanks anyway. – Swapnil Harkanth Dec 24 '14 at 18:32

1 Answers1

0

Decoder returning null because you are not passing valid data. First convert blob into byteArray

Blob blob = c.getBlob(1);
int blobLength = (int) blob.length();  
byte[] outImage = blob.getBytes(1, blobLength);
Log.d("The out image is", String.valueOf(outImage));
                Bitmap bitmap = BitmapFactory.decodeByteArray(outImage , 0, outImage .length);
                iv.setImageBitmap(bitmap);

blob.free();
Praveena
  • 6,340
  • 2
  • 40
  • 53
  • I tried your code, it is asking me to change the type of blob to byte[], in the first line! – Swapnil Harkanth Dec 25 '14 at 05:28
  • @SwapnilHarkanth Yes. You are right.. You dont have to convert actually. Please let me know how are you inserting your blob into db – Praveena Dec 25 '14 at 05:56
  • I am using a simple query to insert the blob into the db. actually i convert the photo taken by the camera into a byte[] and then insert it into db using a query. – Swapnil Harkanth Dec 25 '14 at 06:02
  • 1
    I am suspecting your byte array. Do one thing. Compare your this byte array `byte[] outImage=c.getBlob(1);` with byte array returned from camera. Just to clarify you are getting real data. You can compare two byte array using `Arrays.equal(first,second)` – Praveena Dec 25 '14 at 06:06
  • Hey Praveen, I tried as you said, and the arrays are not matching. Do you have any idea why this is happening? I have updated the question and inserted the code which I am using to insert byte[] in the db. Please have a look. Thanks – Swapnil Harkanth Dec 26 '14 at 17:04