When I take a picture with the camera and then I want to show this image in a ImageView, I was following the next method:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
I was getting the image and showing it in the ImageView correctly, but I have realized that the image obtained was of very low quality. After some research I found that the image obtained with this method is the thumbnail of the image taken. So I modified the code following some indications from other SO posts talking about this:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "Pictures/timeStamp.jpg";
takenPicUri = Uri.fromFile(new File(imageFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, takenPicUri);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
But now, the image is not being shown in the ImageView(pic_view). In the other posts I have read people report that this method worked for them, but is not working for me. I'm forgetting something or I'm doing something wrong?
Well, I have read in old posts that this way of doing this could cause trouble because there was some bug related to it but is the only way in that I have achieved to make it work.
Is as simple as this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Uri selectedImageUri = data.getData();
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
This way is working on android 5.0 and 4.4.4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With