user1125258
user1125258

Reputation:

saving image into SQLITE?

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,"+
            ");"
              );

}

Upvotes: 1

Views: 28086

Answers (3)

Nitesh Khosla
Nitesh Khosla

Reputation: 875

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));

Upvotes: 11

Altaaf
Altaaf

Reputation: 527

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.

Upvotes: 5

Li-aung Yip
Li-aung Yip

Reputation: 12486

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.

Upvotes: 8

Related Questions