0

I created an activity, which takes a photo using the default camera app. I used the code from this tutorial: https://developer.android.com/training/camera/photobasics#TaskPath

I create the intent like this:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

And then I set the path of the output:

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoPath);

In the tutorial, they save currentPhotoPath to a private variable in the activity. I would like to avoid that, and get the path from the intent in the onActivityResult function, here:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        // How to access the path here?
    }
}

I tried intent.getStringExtra(MediaStore.EXTRA_OUTPUT), but it returns null.

I would like to avoid storing the path in a class member, because the activity might be destroyed and recreated by the android system, and then that variable will be null.

I was able to find a similar question, but the answers are for Android 2, and they use deprecated apis, like managedQuery.

Iter Ator
  • 8,226
  • 20
  • 73
  • 164

1 Answers1

0

I would like to avoid that, and get the path from the intent in the onActivityResult function, here

That is not possible, sorry. It would require rewriting every camera app.

I would like to avoid storing the path in a class member, because the activity might be destroyed and recreated by the android system, and then that variable will be null.

Save it in the saved instance state Bundle. This sample app is a bit old, but it demonstrates the basic technique. Save the File in onSaveInstanceState():

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

...and get it back in onCreate(), if the passed-in savedInstanceState Bundle is not null:

output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491