Taking the full-size image from a camera app

My App has an option to take photos from the camera. I'm using phone's default camera. The problem is when I take the picture, in my onActivityResult, I get null from intent(data). so it shows the message Picture wasn't taken!".

Code to call Intent:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Create a File reference to access to future access
            photoFile = getPhotoFileUri(photoFileName);

            // wrap File object into a content provider
            // required for API >= 24
            // See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
            Uri fileProvider = FileProvider.getUriForFile(MainActivity.this, "com.example.android.fileprovider", photoFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider);

            // If you call startActivityForResult() using an intent that no app can handle, your app will crash.
            // So as long as the result is not null, it's safe to use the intent.
            if (intent.resolveActivity(getPackageManager()) != null) {
                // Start the image capture intent to take photo
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {

// by this point we have the camera photo on disk
            Bitmap takenImage = BitmapFactory.decodeFile(String.valueOf(photoFile));
            // RESIZE BITMAP, see section below
            // Load the taken image into a preview
        } else { // Result was a failure
            Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();

Make a file for the photo:

public File getPhotoFileUri(String fileName) {
    // Only continue if the SD Card is mounted
    if (isExternalStorageAvailable()) {
        // Get safe storage directory for photos
        // Use `getExternalFilesDir` on Context to access package-specific directories.
        // This way, we don't need to request external read/write runtime permissions.
        File mediaStorageDir = new File(
                getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
            Log.d(APP_TAG, "failed to create directory");

        // Return the file target for the photo based on filename
        File file = new File(mediaStorageDir.getPath() + File.separator + fileName);

        return file;
    return null;

provider tag in AndroidManifest.xml



    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.houman.myapplication/files/Pictures" />

After "Taking Photos Simply | Android Developers"

didn't help me so much, I tried Accessing the Camera and Stored Media | CodePath

but now I have this problem.

1 answer

  • answered 2018-01-11 20:56 CommonsWare

    I get null from intent(data)

    You are not using data, and in the EXTRA_OUTPUT scenario there is no need for the camera app to supply an Intent in the result. So, get rid of that check, by replacing:

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {


    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {