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.
Example:
The DBAdapter .java file
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.database.sqlite.SQLiteStatement;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
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) {
db.execSQL(USERDETAILS);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertUserDetails(String uname,String userid, String pass, byte[] photo,String visibility) {
ContentValues initialValues = new ContentValues();
initialValues.put("username", uname);
initialValues.put("userid",userid);
initialValues.put("password", pass);
initialValues.put("photo",photo);
initialValues.put("visibility",visibility);
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);
args.put("photo",photo);
args.put("visibility",visibility);
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);
db.open();
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);
image_view.setImageBitmap(theImage);
4 comments:
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.
please sent full code
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea.
here by i also want to share this.
Java training in Chennai
Java training in Bangalore
Java online training
Java training in Pune
Post a Comment