title: How do I get the camera2 api to work a second time?
body: <p>I'm using Xamarin/Android (not Forms), trying to integrate the camera2basic api sample into my project.
  <a href="https://developer.xamarin.com/samples/monodroid/android5.0/Camera2Basic/" rel="nofollow noreferrer">https://developer.xamarin.com/samples/monodroid/android5.0/Camera2Basic/</a></p>
  
  <p>I changed nothing in the sample, and I am only interested in using the main camera and only taking a snapshot.</p>
  
  <p>My project has a MainActivity and the camera2 is one of its Fragments that I'm calling like this:</p>
  
  <pre><code> string fragmentTag = this.Resources.GetString(Resource.String.camera_form);
  
   // Begin the transaction
   FragmentTransaction trans = this.FragmentManager.BeginTransaction();
  
   // Replace the old fragment with the new one.
   trans.Add(Resource.Id.fragment_container, camera2BasicFragment, fragmentTag);
  
   // Add the transaction to the back stack.
   // The tag is added so we can use PopBackStack to skip a screen on the back key
   trans.AddToBackStack(fragmentTag);
  
   // Don't forget to commit
   trans.Commit();
  </code></pre>
  
  <p>Everything works the first time. It takes a photo and saves it to a folder.</p>
  
  <p>The second time I run it, it shows a preview, then when I take a photo it crashes here, where the throw is:</p>
  
  <pre><code>  public void CaptureStillPicture()
    {
       try
       {
          var activity = Activity;
          if (null == activity || null == mCameraDevice)
          {
             return;
          }
          // This is the CaptureRequest.Builder that we use to take a picture.
          if (stillCaptureBuilder == null)
             stillCaptureBuilder = mCameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);
  
          stillCaptureBuilder.AddTarget(mImageReader.Surface);
  
          // Use the same AE and AF modes as the preview.
          stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
          SetAutoFlash(stillCaptureBuilder);
  
          // Orientation
          int rotation = (int)activity.WindowManager.DefaultDisplay.Rotation;
          stillCaptureBuilder.Set(CaptureRequest.JpegOrientation, GetOrientation(rotation));
  
          mCaptureSession.StopRepeating();
  
          try
          {
             mCaptureSession.Capture(stillCaptureBuilder.Build(), new CameraCaptureStillPictureSessionCallback(this), null);
          }
          catch (System.Exception e)
          {
             throw;
          }
  
       }
       catch (CameraAccessException e)
       {
          e.PrintStackTrace();
       }
    }
  </code></pre>
  
  <p>With this error:</p>
  
  <pre><code>{Java.Lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
    at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in &lt;286213b9e14c442ba8d8d94cc9dbec8e&gt;:0 
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in &lt;286213b9e14c442ba8d8d94cc9dbec8e&gt;:0 
    at Android.Hardware.Camera2.CameraCaptureSessionInvoker.Capture (Android.Hardware.Camera2.CaptureRequest request, Android.Hardware.Camera2.CameraCaptureSession+CaptureCallback listener, Android.OS.Handler handler) [0x00078] in &lt;b781ed64f1d743e7881ac038e0fbdf85&gt;:0 
    at RvsMobileApp.Activities.Camera2BasicFragment.CaptureStillPicture () [0x000b7] in C:\Source\RVS\rvs-mobile-app\src\Rvs.Mobile.App\Activities\Camera2BasicFragment.cs:807 
    --- End of managed Java.Lang.IllegalArgumentException stack trace ---
  java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
      at android.hardware.camera2.CaptureRequest.convertSurfaceToStreamId(CaptureRequest.java:674)
      at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:1066)
      at android.hardware.camera2.impl.CameraDeviceImpl.capture(CameraDeviceImpl.java:936)
      at android.hardware.camera2.impl.CameraCaptureSessionImpl.capture(CameraCaptureSessionImpl.java:173)
      at md5bbb797339b35f7667da89d6634e22c37.CameraCaptureListener.n_onCaptureCompleted(Native Method)
      at md5bbb797339b35f7667da89d6634e22c37.CameraCaptureListener.onCaptureCompleted(CameraCaptureListener.java:37)
      at android.hardware.camera2.impl.CameraCaptureSessionImpl$1.lambda$onCaptureCompleted$3(CameraCaptureSessionImpl.java:640)
      at android.hardware.camera2.impl.-$$Lambda$CameraCaptureSessionImpl$1$OA1Yz_YgzMO8qcV8esRjyt7ykp4.run(Unknown Source:8)
      at android.os.Handler.handleCallback(Handler.java:873)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:214)
      at android.os.HandlerThread.run(HandlerThread.java:65)
  }
      base: {Java.Lang.RuntimeException}
      JniPeerMembers: {Android.Runtime.XAPeerMembers}
  </code></pre>
  
  <p>At first I thought it was a memory leak, so I made sure my fragment was killing itself off.  Here is how I end the fragment when the finished button is pressed:</p>
  
  <pre><code>    case Resource.Id.camera_finished:
         // EventHandler&lt;DialogClickEventArgs&gt; nullHandler = null;
         Activity activity = Activity;
         if (activity != null)
         {
            // Send all of the data to the service
            // SendPhotosAndDataToService();
  
            // Call the paren activitity's back to END this Fragment
            activity.FragmentManager.BeginTransaction().Remove(this).CommitNow();
            //activity.OnBackPressed();
  
         }
         break;
  </code></pre>
  
  <p>Here are my steps to reproduce the error:</p>
  
  <p>Start the Camera (load the fragment)
  See a preview 
  Take a photo
  Return to Main Activity (close fragment)</p>
  
  <p>Start the Camera (load the fragment)
  See a preview 
  Take a photo CRASH!!!</p>
  
  <p>As long as I don't take any photos, I can load and unload the fragment as many times as I need to.</p>
  
  <p>I Googled "CaptureRequest contains unconfigured Input/Output Surface!", and didn't get enough information to really understand the problem.
  I think something is not cleaning itself up after the first run.
  I've been working on this issue for days now.</p>