users23456
users23456

Reputation: 49

Store and retreieve blob from sqllite database for android

I need to know to define blob data type in eclipse. I am developing an app for android and i want to save image and a recording in database. I know have to use blob as the data type. My question is how do i define it in the class and the DBhandler class. Can someone provide me help with codes.

Upvotes: 1

Views: 4772

Answers (1)

jeet
jeet

Reputation: 29199

Blob is used to save byte array in sqlite.

To convert a bitmap to byte array use following code:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail);

ByteArrayOutputStream out = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.PNG, 100, out);

byte[] buffer=out.toByteArray();

To save a byte array in blob type use following code:

   ContentValues cv=new ContentValues();
   cv.put(CHUNK, buffer);       //CHUNK blob type field of your table
   long rawId=database.insert(TABLE, null, cv); //TABLE table name

Read more about Blob by this link

Upvotes: 6

Related Questions