Camera Intent Not Adding Extra

2019-02-27 16:25发布

问题:

I am using an implicit Intent to take a picture. I have been following the work outlined in this tutorial. The issue that I am having is that the extra added to the Intent is not being delivered. Here is the code I'm using:

private void dispatchTakePictureIntent(Context context) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile(this.getActivity());
            } catch (IOException ex) {
                Log.e(TAG, "Error creating file: " + ex.toString());
                //TODO: 2017/1/24 - Handle file not created
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Error")
                        .setMessage(ex.toString());

                final AlertDialog dialog = builder.create();
                dialog.show();

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(context,
                        "com.example.myapp",
                        photoFile);
                //THIS EXTRA IS NOT BEING ADDED TO THE INTENT
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

                galleryAddPic(context, photoFile.getAbsolutePath());
            }
        }
    }

When the onActivityResult method is fired, the Intent is empty. Here is that code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //These extras are empty. I have used the debug tool, and there is nothing in here. 
        Bundle extras = data.getExtras();
        //extras is null
        imageBitmap = (Bitmap) extras.get("data");
        previewImage.setImageBitmap(imageBitmap);
    }
}

Why is the intent empty? What do I need to do to fix this issue?

回答1:

Why is the intent empty?

Because you asked for it to be empty, by including EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request. Quoting the documentation:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

 

What do I need to do to fix this issue?

Either:

  • Get rid of EXTRA_OUTPUT (if you want a thumbnail-sized image), or

  • Stop looking for the "data" extra, and look in the location that you specified in EXTRA_OUTPUT