Reputation: 11
How can I store camera click picture in SQLite database?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super n0m .onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode== AppCompatActivity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
//theImage = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
long t=handler.insertImage(byteArray);
if (t > 0) {
Toast.makeText(Camera.this,"sucess image",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Camera.this,"fail image",Toast.LENGTH_LONG).show();
}
// Intent intent = new Intent(Camera.this, Voice.class);
//startActivity(intent);
}
Upvotes: 1
Views: 36
Reputation: 45
you can save images by converting them in the byte array.
I have 2 edit text fields so ignore them
save.setOnClickListener(v -> {
byte[] img = convert_imaveView_ByteArray(imageView);
db_class = new DatabaseClass(MainActivity.this);
if (db_class.save(1,et_name.getText().toString(),et_email.getText().toString(),img)){
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}else Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
db_class.save(1,et_name.getText().toString(),et_email.getText().toString(),img);
});
use this method in your database class
public boolean save(int id, String name, String email,byte [] img){
try{
ContentValues cv = new ContentValues();
cv.put("id",id);
cv.put("name",name);
cv.put("email",email);
cv.put("img",img);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TableName,null,cv);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
Upvotes: 0
Reputation: 13
SQlite supports blob if you want to save the images in database of your device application. But the idea is not to extensively use local database for storing images instead you can save the image in your external storage and then save the path in your sqlite database which will make more sense and efficient.
Upvotes: 1