-5

I have to save an Image captured by Camera in SQLITE DB?Can Any one help me?I am New in Android.

RkshSingh
  • 3
  • 4

4 Answers4

3

We always just save the url of the image in db.

However,you can also get the bytes of the image and insert the data in db(which column is a Blob).

tianwei
  • 1,859
  • 1
  • 15
  • 24
0

You can save your image in BLOB format...

Or. You can convert it using Base64 and save it in CLOB format..

check out this link for ref

I agree with @tianwei, saving Images in to Sqlite is bad idea. You should save its path in to sqlite. Images are big in size comparing to textual information.

It would be hard for saving and retrieving images from sqlite.

So, I would like suggest you to store images in external folder and store its path in to sqlite database.

Community
  • 1
  • 1
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
0

use "blob" to store image. You need to store that image in byte Array.

to store a image you can use this code:

public void insertImg(int id , Bitmap img ) {   


    byte[] data = getBitmapAsByteArray(img);    
    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);

    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ; 
}

 public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}
Oli
  • 3,496
  • 23
  • 32
0

Create bitmap of your image then

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

dba.open();

dba.insertPhoto(byteArray);

where dba is object of database class.

create table in database class like:

private static final String CREATETABLE_PHOTO = "create table eqpphoto("EImage BLOB " + ");";

public static final String TABLE_PHOTO = "eqpphoto";

public long insertPhoto(byte[] EImage) {

        try {
            System.out.println("Function call : ");
            ContentValues values = new ContentValues();

            values.put(EIMAGE, EImage);
            return db.insert(TABLE_PHOTO, null, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99