Reputation: 67
Trying to make a button takes captures a picture and displays it in imageview. I am saving that image temporary to have better quality. Camera doesn't work but nothing is showing on imageview
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.l_sliuzas.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && requestCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Upvotes: 1
Views: 57
Reputation: 82938
Here REQUEST_TAKE_PHOTO
is a requestCode
and RESULT_OK
is resultCode
. But inside onActivityResult
, you are evaluating both with requestCode
. Which is not correct.
So below code is not correct
if (requestCode == REQUEST_TAKE_PHOTO && requestCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}
Change it to
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}
Upvotes: 2