Home > Android > Store Image in SQLite database

Store Image in SQLite database


How to store Image in SQLite database? Have you ever asked your self? If you have, this is a solution for you to store image in SQLite DB.

Just simple as simple.😉

So here is an example how to store some data (bitmap included) to a local DB
and how to get the data back again.

First the Class that defines a Fruit
Fruit.java

package de.padde.DBtest;

import android.graphics.Bitmap;

public class Fruit {

private Bitmap bmp;

private String name;

private int kcal;

private int vitaminc;

public Fruit(Bitmap b, String n, int k, int v) {

bmp = b;

name = n;

kcal = k;

vitaminc = v;

}

public Bitmap getBitmap() { return bmp; }

public String getName() { return name; }

public int getKcal() { return kcal; }

public int getVitaminC() { return vitaminc; }

}

Nothing special here..

Now a Class that helps us handle the database
DBhelper.java

package de.padde.DBtest;

import java.io.ByteArrayOutputStream;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.SQLException;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class DBhelper {

public static final String KEY_ID = “id”;

public static final String KEY_NAME = “name”;

public static final String KEY_KCAL = “kcal”;

public static final String KEY_VC = “vitaminc”;

public static final String KEY_IMG = “image”;

private DatabaseHelper mDbHelper;

private SQLiteDatabase mDb;

private static final String DATABASE_NAME = “DBtest”;

private static final int DATABASE_VERSION = 1;

private static final String FRUITS_TABLE = “fruits”;

private static final String CREATE_FRUITS_TABLE = “create table “+FRUITS_TABLE+” (”

+KEY_ID+” integer primary key autoincrement, ”

+KEY_IMG+” blob not null, ”

+KEY_NAME+” text not null unique, ”

+KEY_KCAL+” integer not null, ”

+KEY_VC+” integer not null);”;

private final Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_FRUITS_TABLE);

}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL(“DROP TABLE IF EXISTS “+FRUITS_TABLE);

onCreate(db);

}

}

public void Reset() { mDbHelper.onUpgrade(this.mDb, 1, 1); }

public DBhelper(Context ctx) {

mCtx = ctx;

mDbHelper = new DatabaseHelper(mCtx);

}

public DBhelper open() throws SQLException {

mDb = mDbHelper.getWritableDatabase();

return this;

}

public void close() { mDbHelper.close(); }

public void createFruitEntry(Fruit fruit) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

fruit.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);

ContentValues cv = new ContentValues();

cv.put(KEY_IMG, out.toByteArray());

cv.put(KEY_NAME, fruit.getName());

cv.put(KEY_KCAL, fruit.getKcal());

cv.put(KEY_VC, fruit.getVitaminC());

mDb.insert(FRUITS_TABLE, null, cv);

}

public Fruit getFirstFruitFromDB() throws SQLException {

Cursor cur = mDb.query(true,

FRUITS_TABLE,

new String[] {KEY_IMG, KEY_NAME, KEY_KCAL, KEY_VC},

null, null,null, null, null, null);

if(cur.moveToFirst()) {

byte[] blob = cur.getBlob(cur.getColumnIndex(KEY_IMG));

Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);

String name = cur.getString(cur.getColumnIndex(KEY_NAME));

int kcal = cur.getInt(cur.getColumnIndex(KEY_KCAL));

int vc = cur.getInt(cur.getColumnIndex(KEY_VC));

cur.close();

return new Fruit(bmp,name,kcal,vc);

}

cur.close();

return null;

}

}

The “magic” happens here in the createFruitEntry and getFirstFruitFromDB methods.
I used a small code version to convert a bitmap to a byte array for storage as blob.. but i doubt that
this is the best or fastest way to do this.

The last Class is the Activity that shows usage of the DBhelper on the basis of an example.
DBtest

package de.padde.DBtest;

import android.app.Activity;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

public class DBtest extends Activity {

private DBhelper DbHelper;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);

ImageView image = new ImageView(this);

TextView text = new TextView(this);

DbHelper = new DBhelper(this);

Fruit testFruit = new Fruit(

BitmapFactory.decodeResource(getResources(), R.drawable.icon),

“Icon”, 100, 0);

DbHelper.open();

DbHelper.createFruitEntry(testFruit);

DbHelper.close();

testFruit = null;

DbHelper.open();

testFruit = DbHelper.getFirstFruitFromDB();

DbHelper.close();

image.setImageBitmap(testFruit.getBitmap());

text.setText(“Name: “+testFruit.getName()+”, ”

+testFruit.getKcal()+”kcal, ”

+testFruit.getVitaminC()+”mg Vitamin C”);

ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

setContentView(layout);

addContentView(image, params);

addContentView(text, params);

}

}

Here again nothing special..

End result you will see in you database like this:

Source :anddev.org

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: