Hamzah Abdullah Hamzah Abdullah - 1 year ago 128
Android Question

Android Save And Get Image From Sqlite Database

How I can Android Save Image And Get Image From Sqlite Database I'm Using Android Studio ?

Answer Source

You can check this tutorial for implementing Sqlite in Android. http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Instead working with Contact for example, you can implement this way

public class Image{
    String imagePath; //it is your absolute image file path

And your DatabaseHandler can be implemented like below

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "imagedb";

    // Contacts table name
    private static final String TABLE_IMAGE = "images";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_IMAGE_PATH = "name";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    // Creating Tables
    public void onCreate(SQLiteDatabase db) {
        String sqlQuery = "CREATE TABLE " + TABLE_IMAGE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_IMAGE_PATH + " TEXT)";

    // Upgrading database
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed

        // Create tables again

    // Adding new image
    public void addImage(Image image) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_IMAGE_PATH, image.imagePath); // Image path

        // Inserting Row
        db.insert(TABLE_IMAGE, null, values);
        db.close(); // Closing database connection

    // Getting single image
    public Image getImage(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_IMAGE, new String[] { KEY_ID,
                        KEY_IMAGE_PATH}, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)

        Image image = new Image(Integer.parseInt(cursor.getString(0)),
        // return image
        return image;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download