@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_read_palm:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
break;
case R.id.btn_read_from_existing:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMG);
break;
}
}
Here getting selectedImageUri = content://media/external/images/media/2997 and
path = /storage/emulated/0/DCIM/Camera/IMG_20170510_132342860.jpg
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_REQUEST:
Bitmap photo = (Bitmap) data.getExtras().get("data");
saveAndShowPictureDialog(photo);
break;
case RESULT_LOAD_IMG:
Uri selectedImageUri = data.getData();
String path = getRealPathFromURI(this, selectedImageUri);
if (path != null)
showImageDialog(path);
}
}
}
getRealPathFromURI function returning correct path.
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private void showImageDialog(String pictureFile) {
// custom dialog
final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.image_dialog);
dialog.setTitle("Image");
// find the imageview and draw it!
ImageView image = (ImageView) dialog.findViewById(R.id.image);
Bitmap bmImg = BitmapFactory.decodeFile(pictureFile);
here i am getting a black screen/image on imageview when setting Image Bitmap from the picture file.
image.setImageBitmap(bmImg);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}