I've been looking for quite many tutorial to find out what is the best way to save image to SQLite
. At last I decided to get the imagePath and save it into SQLite
, and display the captured image into ImageView
.
private void activeTakePhoto() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
try {
String selectedImage = getRealPathFromURI(mCapturedImageURI);
imageView.setImageDrawable(Drawable.createFromPath(selectedImage));
} catch (Exception e) {
Toast.makeText(getApplication(),"Failed",Toast.LENGTH_SHORT).show();
}
break;
case RESULT_LOAD_IMAGE:
//
break;
}
}
}
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
I did a small test to print the selectedImage and value get displayed, but my imageView is black color.