In my application I am uploading an image from gallery and I want to store this image in the SQLite database. How do I store a bitmap in the database? I am converting bitmap
Don't forget to write user permission code in Manifest file
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CameraActivity = this;
imageView = (ImageView) findViewById(R.id.image_view);
database = new ImageDatabase(this);
//Set OnClick Listener to button view
captureImage = (Button) findViewById(R.id.capture_image);
captureImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
theImage = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
theImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
SQLiteDatabase db = database.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ImageDatabase.KEY_IMG_URL, byteArray);
db.insert(ImageDatabase.TABLE_NAME, null, values);
db.close();
Bitmap b = getTheImage();
imageView.setImageBitmap(b);
}
}
public Bitmap getTheImage(){
SQLiteDatabase db = database.getReadableDatabase();
Cursor cursor = (Cursor) db.rawQuery(" SELECT * FROM "+ImageDatabase.TABLE_NAME,null,null);
if (cursor.moveToFirst()){
byte[] imgByte = cursor.getBlob(cursor.getColumnIndex(ImageDatabase.KEY_IMG_URL));
cursor.close();
return BitmapFactory.decodeByteArray(imgByte,0,imgByte.length);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return null;
}
}
DATABASE CLASS
class ImageDatabase extends SQLiteOpenHelper {
public Context context;
public static final String DATABASE_NAME = "dataManager";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "data";
public static final String KEY_ID = "id";
public static final String KEY_IMG_URL = "ImgFavourite";
public ImageDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
//Toast.makeText(context, "Constructor called", Toast.LENGTH_LONG).show();
}
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG_URL + " BLOB " + ")";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME + "";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
public void deleteEntry(long row) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.delete(TABLE_NAME, KEY_ID + "=" + row, null);
}
}