Core Android

Switch Camera in Camera2 API Android

1. Switch camera rear to front & front-rear in Camera2 API

In the previous 2 blogs, I have explained Video Recoding using Camera2 API. Furthermore, In this tutorial I’m explaing about swich camera front-rear via-varsa. As you we have build a utility class.

2. Declare below variable in

   * 0 forback camera
   * 1 for front camera
   * Initlity default camera is front camera
  public static final String CAMERA_FRONT = "1";
  public static final String CAMERA_BACK = "0";
  private String cameraId = CAMERA_FRONT;

3. Check available cameras

We can fetch all available cameras from Camera Manager instance by calling getCameraIdList.It returns all available via getCameraIdList() (front/rear), then you can find the best camera that you want to use or suits your needs.

You can get CameraCharacteristics by using camera ID. You can manage facing, resolution, and filter also. In variable declare block, we define front and back camera id.

In this tutorial, the front camera is the default camera.

CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

As above, private String cameraId = CAMERA_FRONT; we set default camera is front camera, So cameraId have instance of front camera.

4. Furthermore, we can Switch Camera using below code

 public void switchCamera() {
        if (cameraId.equals(CAMERA_FRONT)) {
            cameraId = CAMERA_BACK;

        } else if (cameraId.equals(CAMERA_BACK)) {
            cameraId = CAMERA_FRONT;


After changing cameraId we just close the previous camera and reopen it.

 public void reopenCamera() {
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
Finally just call switchCamera() method in child Fragment

After following all above just RUN the project and use app, If you have any queries, feel free to ask them in the comment section below. Happy Coding 🙂

Download Sample Project- Switch Camera in Camera2 API Android
Notify of
Inline Feedbacks
View all comments