title: Android Camera2 API switch back - front cameras
body: <p>I'm creating a custom camera capturing videos with the new camera2 API.</p>
  
  <p>My code is strongly inspired from the code provided by Google <a href="https://github.com/googlesamples/android-Camera2Video" rel="noreferrer">here</a>. My camera preview has a button to switch from back to front camera and then from front to back camera.
  The "camera preview" activity is launched with the back camera by default.</p>
  
  <p>For some reason, when I click on the "switch/swap camera" button <strong>for the first time</strong>, it brings be to the front camera as it should, BUT everytime I click again, the switch/swap doesn't work anymore: the preview (on the front camera) fades a little bit, like if something is happening, but it remains on the currently selected (front) camera. </p>
  
  <p>Here is my code :</p>
  
  <p>In a <code>RecordVideoFragment</code>, in the <code>onViewCreated</code>:</p>
  
  <pre><code>//  Switch camera button
          switchCameraButton = (ImageButton) view.findViewById(R.id.button_switch_camera);
          // Listener for Switch cameras button
          switchCameraButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  switchCameras();
              }
          });
  </code></pre>
  
  <p>And here is the <code>switchCameras()</code> function: </p>
  
  <pre><code>private void  switchCameras() {
          mCameraOpenCloseLock.release();
          mCameraDevice.close();
  
          CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
          try {
              String mCameraId = mCameraManager.getCameraIdList()[0];
              if (mCameraId.equals("0")) {   // If currently on FRONT camera (0 = CameraCharacteristics.LENS_FACING_FRONT)
                  mCameraId = "1";           // switch to BACK camera (1 = CameraCharacteristics.LENS_FACING_BACK)
                  switchCameraButton.setImageResource(R.drawable.ic_camera_front);
              } else {  // If currently on BACK camera
                  mCameraId = "0"; // switch to front camera
                  switchCameraButton.setImageResource(R.drawable.ic_camera_back);
              }
              try {
                  if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                          == PackageManager.PERMISSION_GRANTED) {
                      mCameraManager.openCamera(mCameraId, mStateCallback, null);
                  } else {
                      requestVideoPermissions();
                  }
              } catch (CameraAccessException e) {
                  e.printStackTrace();
              }
          } catch (CameraAccessException e) {
              Toast.makeText(getActivity(), "Cannot access the camera.", Toast.LENGTH_SHORT).show();
              getActivity().finish();
          }
      }
  </code></pre>
  
  <p>If you have any idea on what's happening that would save me. I have been bugging on this for days. Thank you very much</p>