1

which data type should I use for storing an image in sqlLite database table??

the following is my attempt:

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE" + MP_TABLE_NAME + " ( " +
            BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, " +
            " LAT REAL, "+
            " LNG REAL,"+
            ");"
              );

}
  • 5
    You should store the image on the SD card and in the database you store the path to the image. – user Feb 16 '12 at 09:54
  • is not easily to save images to internal/external memory and just save some reference to them in database? – goodm Feb 16 '12 at 09:55

3 Answers3

11

Try this..

Creating a table with a BLOB column:

CREATE TABLE storedImages (_id INTEGER PRIMARY KEY, myImage BLOB);

Downloading an image using HTTP and storing it in your table:

DefaultHttpClient mHttpClient = new DefaultHttpClient();  
HttpGet mHttpGet = new HttpGet("your image url");  
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);  
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
    HttpEntity entity = mHttpResponse.getEntity();  
    if (entity != null) {  
        // insert to database  
        ContentValues values = new ContentValues();  
        values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));  
        getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);  
    }
 }

Loading the BLOB into an ImageView:

 ImageView myImage = (ImageView) findViewById(R.id.myImage);
 byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
 myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
Jens
  • 16,853
  • 4
  • 55
  • 52
Nitesh Khosla
  • 875
  • 8
  • 20
8

Refer to the SQLite documentation on datatypes.

You probably want a BLOB type, also known as "binary blob" or "Binary Long OBject". Just be aware that storing really big things in an SQLite database isn't necessarily a good idea - images are usually better off stored in the filesystem if you can manage it.

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
5

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.

Altaaf
  • 527
  • 3
  • 11