Skip to content

Instantly share code, notes, and snippets.

@yeradis
Created March 28, 2011 16:34
Show Gist options
  • Save yeradis/890783 to your computer and use it in GitHub Desktop.
Save yeradis/890783 to your computer and use it in GitHub Desktop.
ANDROID - Store and read an image on SQlite
To store an image file inside your SQLite db you should use a Blog field, and due Android SQlite limitations you should store info in this way:
ContentValues cv = new ContentValues();
//your table fields goes here
...
// adding the bitmap in byte array way to the blob field
ByteArrayOutputStream out = new ByteArrayOutputStream();
friendInfo.getImage_bmp().compress(Bitmap.CompressFormat.PNG, 100,out);
cv.put("avatar_img", out.toByteArray());
db.insert(TABLE_NAME, null, cv);
and to read it you can do:
byte[] blob = cur.getBlob(cur.getColumnIndex("avatar_img"));
Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0,blob.length, opts);
friend.setImage_bmp(bmp);
friend is an instance from a class, having a Bitmap property called Image_bmp ;)
@ankita1989
Copy link

Provide some example please

@srb9181
Copy link

srb9181 commented Apr 7, 2016

how we can retrieve image file from database to imagview

Copy link

ghost commented Sep 28, 2017

.compress()==> cannot resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment