1

I am trying to save image in sqlite database and then retrieve it later. I have stored it in form of byte[]. BUt bitmap is not being formed!

Storing images: (blob type)

Drawable myDrawable = getResources().getDrawable(arr[i]);
myLogo = ((BitmapDrawable) myDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();

My code for retrieving:

byte[] Image;
Image = c.getBlob(c.getColumnIndex("img_str"));
BitmapFactory.Options options = new BitmapFactory.Options();
decodedByte = BitmapFactory.decodeByteArray(Image, 0,Image.length, options);
arr_img.add(decodedByte);

System.out.println("Image = " + Image);
System.out.println("decodedByte = " + decodedByte);

Logcat:

07-22 07:15:55.482: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.482: I/System.out(19319): Image = [B@4057ea48
07-22 07:15:55.482: I/System.out(19319): decodedByte = null
07-22 07:15:55.521: D/dalvikvm(19319): GC_EXPLICIT freed 114K, 50% free 2871K/5639K,external 5406K/5783K, paused 43ms
07-22 07:15:55.541: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.541: I/System.out(19319): Image = [B@40577aa0
07-22 07:15:55.541: I/System.out(19319): decodedByte = null

please help as i am clueless why this is not working. thanks

Carbon
  • 133
  • 1
  • 4
  • 21

2 Answers2

1

Your image bytes are very small. Are you sure that is the whole image? Accoring to your output the image is only 22 bytes and that is small. I only say this because the API states:

Returns The decoded bitmap, or null if the image could not be decode.

I believe that image is not as large as it is supposed to be.

Try to store your image again in the database with this code and try to retrieve again:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();
ObieMD5
  • 2,684
  • 1
  • 16
  • 26
0

Actually the problem was not with the conversion but the way how i was storing it..

I used prepared statement to insert values.. bindBlob() did the job

String sql = "INSERT INTO demoImage (img_name,img_str) VALUES(?,?)";

    SQLiteStatement insertStmt = dbsqlite.compileStatement(sql);
    insertStmt.clearBindings();
    insertStmt.bindString(1, name);
    insertStmt.bindBlob(2, bal);
    insertStmt.executeInsert();
Carbon
  • 133
  • 1
  • 4
  • 21