0
votes

I'm trying to capture an image, but after capturing and approving, onActivityResult(int requestCode, int resultCode, Intent data) the data is always null .

This is how I call the camera:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(intent, Consts.ACTION_JOURNEY_CAPTURE_PHOTO_PATH);

Method getImageUri():

 private Uri getImageUri() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File albumF = helpers.getAlbumDir(getString(R.string.album_name));

        File file = new File(albumF, imageFileName);
        Uri imgUri = Uri.fromFile(file);

        return imgUri;
}

On manifest I have :

<uses-feature android:name="android.hardware.camera" />

What am I doing wrong?

1
data returns a thumbnail (very small) image that may be used to give e general idea what the captured image is about. Some devices may not generate thumbnail at all, or only for the main (back) camera. data == null is not an indication of error. - Alex Cohn

1 Answers

1
votes

The image is stored at the path that you get with the method getImageUri(). You must keep that path and inside onActivityResult() do the following:

    if (resultCode == RESULT_OK) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(photoPath, options);

        Bitmap b = BitmapFactory.decodeFile(photoPath, options);

    }

If you want to resize the image, you can set the inSampleSize of your BitmapFactory.Options, this method will be useful to calculate the inSampleSize:

 private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }