-3

I want to store image in sqlite db in android.My question is

1)do I need to convert image to blob before inserting or can simply insert bitmap/byte[] that android may covert to blob and store?

2)If I need to convert to blob before storing then how can I convert byte[] to blob .....(I learned to convert drawable to bitmap to byte[])?

I am new to android..please help..

Bharat
  • 2,441
  • 3
  • 24
  • 36
Swapnil
  • 137
  • 2
  • 10
  • http://stackoverflow.com/questions/32572993/is-it-a-good-idea-to-store-bitmap-as-blob-in-sqlite – Andrew Nguyen Nov 28 '16 at 06:37
  • 1
    Possible duplicate of [How to store image in SQLite database](http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database) – Nakul Nov 28 '16 at 06:41

2 Answers2

4

Store image in sqlite:

Convert bitmap to byte array.

public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

Insert Image byte array (blob) in Database:

public void addEntry( String name, byte[] image) throws SQLiteException{
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues cv = new  ContentValues();
    cv.put(KEY_IMAGE,   image);
    database.insert( DB_TABLE, null, cv );
}

Retrieve image from sqlite:

Retrieving byte array (blob) data from database :

byte[] image = cursor.getBlob(1); // set your column index instead of 1

Convert byte array to bitmap.

public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}
Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
0

Store an Image from Android to a SQlite and retrieve it

DBHelper.java

public class DBHelper {

public static final String IMAGE_ID = "id";
public static final String IMAGE = "image";
private final Context mContext;

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DATABASE_NAME = "Images.db";
private static final int DATABASE_VERSION = 1;

private static final String IMAGES_TABLE = "ImagesTable";

private static final String CREATE_IMAGES_TABLE =
        "CREATE TABLE " + IMAGES_TABLE + " (" +
                IMAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + IMAGE + " BLOB NOT NULL );";


private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_IMAGES_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_IMAGES_TABLE);
        onCreate(db);
    }
}

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

public DBHelper(Context ctx) {
    mContext = ctx;
    mDbHelper = new DatabaseHelper(mContext);
}

public DBHelper open() throws SQLException {
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

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

// Insert the image to the Sqlite DB
public void insertImage(byte[] imageBytes) {
    ContentValues cv = new ContentValues();
    cv.put(IMAGE, imageBytes);
    mDb.insert(IMAGES_TABLE, null, cv);
}

// Get the image from SQLite DB
// We will just get the last image we just saved for convenience...
public byte[] retreiveImageFromDB() {
    Cursor cur = mDb.query(true, IMAGES_TABLE, new String[]{IMAGE,},
                           null, null, null, null,
                           IMAGE_ID + " DESC", "1");
    if (cur.moveToFirst()) {
        byte[] blob = cur.getBlob(cur.getColumnIndex(IMAGE));
        cur.close();
        return blob;
    }
    cur.close();
    return null;
   }
}

Utils.java

public class Utils {

public static byte[] getImageBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

public static byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
  }

}

you can follow this complete tutorial to store/retrieve image from sqlite Store an Image from Android to a SQlite and retrieve it

Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53