21

My issue is as follows : I have stored a few pictures into the sqlite database, using the blob format, which seems to work ok. now i want to get my pictures out of the DB and put then back into images... to complicate the matter, their format is variable (png, jpg, maybe something else, im not sure) Is there a way of doing so in android?

thank you

Sephy
  • 50,022
  • 30
  • 123
  • 131

3 Answers3

53

Use BitmapFactory.decodeByteArray() method:

byte[] blob=c.getBlob("yourcolumnname");
Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length);
ImageView image=new ImageView(this);
image.setImageBitmap(bmp);

Look at this thread too.

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • that's exactly what i did, but i was using drawable so I added a cast, but i don't think i need it. thanks for the help guys. thanks for the detailed explanation here – Sephy Apr 26 '10 at 16:12
  • decodeByteArray method return null if the image could not be decoded; check your code that store to db. – systempuntoout Apr 26 '10 at 18:27
  • 1
    this doesn't work on motorola! decodeByteArray says that I should send valid data it is not image data that I am sending. why? how to fix it? – Toshe Feb 08 '12 at 17:07
4

Use BitmapFactory.decodeByteArray().

Chad Birch
  • 73,098
  • 23
  • 151
  • 149
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
4

I prefer to convert the array of bytes to Drawable directly. It is the best format to use in Android. Bitmaps generated leaks in the past.

Drawable d = Drawable.createFromStream(new ByteArrayInputStream(ARRAY_BYTES), null);
J.S.R - Silicornio
  • 1,111
  • 13
  • 16