Monday, 3 October 2011

Store the Image into SQLite Database

In this tutorial, I used the BLOB data type to store the image into the SQLite  database.
BLOB --> Binary Large Object.
This data type is used to store the values in the binary format. This type is used to store the images ,audio and video file.
Here I am going to store the image .So that ,
1.I have to convert the image to byte[].
2.This byte[] is going to be stored in the database.

The DBAdapter .java file


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.database.sqlite.SQLiteStatement;

public class DBAdapter {         
    private static final String DATABASE_NAME = "myDatabase.db";
    private static final int DATABASE_VERSION = 1;

    private static final String USERDETAILS=
                "create table userdetails(usersno integer primary key autoincrement,userid text not null ,username text not null,password text not null,photo BLOB,visibility text not null);";
    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    public DBAdapter(Context ctx)     {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    private static class DatabaseHelper extends SQLiteOpenHelper     {
        DatabaseHelper(Context context)         {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        public void onCreate(SQLiteDatabase db)         {
        public void onUpgrade(SQLiteDatabase db, int oldVersion,    int newVersion)         {
                db.execSQL("DROP TABLE IF EXISTS users");
    public DBAdapter open() throws SQLException
        db = DBHelper.getWritableDatabase();
        return this;
    public void close()     {
    public long insertUserDetails(String uname,String userid, String pass, byte[] photo,String visibility)     {
        ContentValues initialValues = new ContentValues();
        initialValues.put("username", uname);
        initialValues.put("password", pass);
        return db.insert("userdetails", null, initialValues);
 public Cursor getUserDetails(String userid)    {
                return db.rawQuery("select username,userid,photo,visibility ,usersno from userdetails where userid='"+userid+"'",null);
    public int updateUserDetails(String userid,String username,byte[] photo,String visibility)    {
                ContentValues args = new ContentValues();
        args.put("username", username);
        return db.update("userdetails", args,"userid='" +userid+"'", null);
    public Cursor searchFriends(String username,String to_userid)    {
                return db.rawQuery("select userid,username,photo,usersno from userdetails where username LIKE '%"+username+"%' AND userid <>'"+to_userid+"' AND userid NOT IN (select request_destinationid from requestdetails where request_sourceid='"+to_userid+"' and status=1)",null);
    public void deleteFriend(int l_userid,int dest_userid)   {
                db.delete("requestdetails","request_sourceid="+l_userid+" and request_destinationid="+dest_userid+" and status=1", null);
                db.delete("requestdetails","request_sourceid="+dest_userid+" and request_destinationid="+l_userid+" and status=1", null);

In  the Activity class write like this
To Store into the DB..
private DBAdapter db=new DBAdaptet(this);;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
Bitmap bitmap = (BitmapDrawable)getResources().getDrawable(R.drawable.common)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
byte[] photo = baos.toByteArray();
db.insertUserDetails(fullName,userid, password, photo,visibility);

Retrieve from DB..
byte[]  photo=cursor.getBlob(..);
ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);


krishnaveniv said...

Hi ....

Your code is very nice.Because itz easily understood...But i can't do developed it..So please post me full source code or send me full project zip file.Thanks in advance.

nazeerahmed said...

please sent full code

a simple person said...
This comment has been removed by the author.
Pavel Co Ebele said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India

Petter Marry said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

Post a Comment