0

On button press, I spawn an intent to take a picture. My goal is to show a thumbnail bitmap on the screen and save the full sized image on the phone, for consumption later.

public void takePicture(View v) {
    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 e) {
            e.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            mImageUri = FileProvider.getUriForFile(this,
                    "com.acme.hello.world.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

This is how I'm responding to the request:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == -1 /*RESULT_OK*/) {

        try {

            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            Log.d(TAG, String.format("%sx%s", thumbnail.getWidth(), thumbnail.getHeight()));
            Bitmap full = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
            Log.d(TAG, String.format("%sx%s", full.getWidth(), full.getHeight()));

            // TODO: Consider deleting the file off the device, or warn when out of memory

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

The problem is that data.getExtras() is returning null.

Not sure what I'm doing wrong as I'm following these instructions provided by Android docs.

EDIT: Looks like Android doesn't written both thumbnail and full image

Carpetfizz
  • 8,707
  • 22
  • 85
  • 146

0 Answers0