1

I'm following android developers guide to capture a high quality image and save it into the storage then display it. The issue is after capturing the image I get redirected to the main activity without displaying the preview though the image was created successfully in the storage

public void take_hq_image(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, HIGH_Q_IMAGE_CAPTURE_REQUEST);
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == HIGH_Q_IMAGE_CAPTURE_REQUEST && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap bitmap = (Bitmap) extras.get("data");
        HQimageView.setImageBitmap(bitmap);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PHP User
  • 2,350
  • 6
  • 46
  • 87
  • You are using `EXTRA_OUTPUT`. As a result, there is no `"data"` extra in the returned `Intent`. Your image should be at `photoFile` -- load it from there. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/FINAL/Camera/FileProvider). – CommonsWare Feb 12 '19 at 14:06
  • Is HQimageView.setImageBitmap(bitmap); called when the camera finishes? Is HQimageView initialised? – Miguel Isla Feb 12 '19 at 14:06

1 Answers1

0

Saved image path as a string in currentImagePath variable

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 */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentImagePath = image.getAbsolutePath();
    return image;
}

Then displayed the preview

Bitmap bitmap = BitmapFactory.decodeFile(currentImagePath);
HQimageView.setImageBitmap(bitmap);
PHP User
  • 2,350
  • 6
  • 46
  • 87