Search code examples
androidandroid-intentandroid-activityandroid-camera-intent

Why does the camera intent not return to calling Activity?


I am creating an implicit android intent. The phone's camera app opens up. However, when I take the photo the camera app closes but the Activity which started the camera intent is not opened. The phone goes to the main screen. If I open the app back up it is still in the camera app. I can click the back button from the camera and go back the Activity.

The intent is started with this line.

startActivityForResult(takePhotoIntent, IMAGE_REQUEST_CODE);

This is the intent I am creating.

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Then I am adding MediaStore.EXTRA_OUTPUT to the intent

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePhotoIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException e) {
                // Error occurred while creating the File
                //TODO
                Log.e(TAG, e.toString());
                return null;
            }
            // Continue only if the File was successfully created
            if (photoFile != null && photoFile.exists()) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.markd.android.fileprovider",
                        photoFile);
                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            } else {
                Log.e(TAG, "photoFile not configured");
            }
        } else {
            Log.e(TAG, "ResolveActivity is null");
        }
    }

Here is the createImageFile method.

private File createImageFile() throws IOException {
    File image = File.createTempFile(
            "home_image_" + UUID.randomUUID().toString(),  /* prefix */
            ".jpg",         /* suffix */
            getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
    );

    if(image.getParentFile().mkdirs()) {
        Log.e(TAG, "mkdirs:true");
    } else {
        Log.e(TAG, "mkdirs:false");
    }
    if(image.exists()) {
        Log.e(TAG, "Image exists");
        Log.e(TAG, "Path:"+image.getAbsolutePath());
    } else {
        Log.e(TAG, "Image does not exist");
    }

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

This is the function which should be called when returns but nothing is logged.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult");
    if (requestCode == IMAGE_REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            //Process result
        } else {
            Log.d(TAG, "Result not okay");
        }
    } else {
        Log.e(TAG, "Unknown Request");
    }
    super.onActivityResult(requestCode, resultCode, data);
}

It is being tested on a Motorola XT1028 Android 5.1, API 22.


Solution

  • Turns out this was operating error. The code was correct. I was hitting the home button which is a soft button that I thought was the button to take the picture. I needed to touch the screen to take a picture. It wasn't a button.