Store and retreieve blob from sqllite database for

2019-04-16 06:37发布

问题:

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.

回答1:

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