Search code examples
androidandroid-cameraandroid-camera-intent

Null result after taking a photo


I need to take a photo, get the full-size file to send to a server. With thumbnails it works fine, but with i can't recover the full-size photo. I read and copied most of the code from google tutorial on android developers web page.

I'm doing this:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        mPhotoFile = null;
        try {
            mPhotoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }            
        if (mPhotoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    mCurrentPhotoPath);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

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;
    if (StorageUtils.isExternalStorageWritable()) {
        storageDir = StorageUtils.getExternalStorageAppDir(getActivity());
    } else {
        storageDir = Environment.getDataDirectory();
    }
    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
    );
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;        
        Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); // returns null
        mImageAdapter.addImage(bitmap);
    }
}

This line (inside onActivityResult returns null):

Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

I read a lot of posts about camera issues but nothing seems to work. I'm doing something wrong?

Thanks in advance.

Note: i test the code in an emulator and in a real device. Same result.


Solution

  • The problem was in this line:

    bmOptions.inJustDecodeBounds = true;
    

    The google doc about bmOptions.inJustDecodeBounds says:

    If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

    After removing this line the image was returned ok.