title: How to change camera from front to back and back to front on button click in android
body: <p>I am working on an android tablet application. I want to change camera from front to back and back to front on button. How can I achieve this ? I have tried several example but not getting proper response. </p>
  
  <p>I am adding code also.</p>
  
  <pre><code>public class PhotoPreview extends Activity implements SurfaceHolder.Callback {
  
  private Camera camera;
  private ImageButton cameraClick;
  private ImageButton cameraSwap;
  SurfaceView surfaceView;
  private SurfaceHolder mHolder;
  boolean previewing = false;
  String path = "";
  LayoutInflater controlInflater = null;
  Bitmap bmp;
  Button cameraCancel;
  private SharedPreferences myPrefs;
  private int camId;
  
  /** Called when the activity is first created. */
  @SuppressWarnings("deprecation")
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      System.out.println("Photo preview Called $$$$$$$$$$$$$$$$$$ ");
  
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
      setContentView(R.layout.photo_preview);
  
      myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
      camId = myPrefs.getInt("camId",1);
  
      System.out.println("CAM ID $$$$$$$$$$$$$$$$$$ "+camId);
  
      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
  
      mHolder = surfaceView.getHolder();
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mHolder.addCallback(this);
  
  
      controlInflater = LayoutInflater.from(getBaseContext());
      View viewControl = controlInflater.inflate(R.layout.control, null);
      LayoutParams layoutParamsControl = new LayoutParams(
              LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
      this.addContentView(viewControl, layoutParamsControl);
      cameraClick = (ImageButton) findViewById(R.id.cameraClick);
      cameraClick.setOnClickListener(cameraClickListener);
      cameraCancel = (Button) findViewById(R.id.cameraCancel);
      cameraCancel.setOnClickListener(cameraCancelClickListener);
      cameraSwap = (ImageButton) findViewById(R.id.cameraSwap);
      cameraSwap.setOnClickListener(swapCameraClickListener);
  
  }
  
  /*
   * This is click event of Photo capture button
   */
  private OnClickListener cameraClickListener = new OnClickListener() {
      @Override
      public void onClick(final View v) {
          if (camera != null) {
              camera.takePicture(shutterCallback, rawCallback, jpegCallback);
          }
  
      }
  };
  
  /*
   * This is click event of camera cancel button
   */
  private OnClickListener cameraCancelClickListener = new OnClickListener() {
      @Override
      public void onClick(final View v) {
  
          Intent intent = new Intent(PhotoPreview.this,
                  MainScreenActivity.class);
          startActivity(intent);
  
      }
  };
  
  /*
   * This is click event of camera cancel button
   */
  private OnClickListener swapCameraClickListener = new OnClickListener() {
      @Override
      public void onClick(final View v) {
  
          if (camId == 0) {
              SharedPreferences.Editor prefsEditor = myPrefs.edit();
              prefsEditor.putInt("camId", 1);
              prefsEditor.commit();
  
          } else {
              SharedPreferences.Editor prefsEditor = myPrefs.edit();
              prefsEditor.putInt("camId", 0);
              prefsEditor.commit();
          }
          System.out.println("CAM ID ^^^^^^^^^^^^^^^^^^^^ "+camId);
          Intent intent = new Intent(PhotoPreview.this, PhotoPreview.class);
          startActivity(intent);
  
      }
  };
  
  // Handles when shutter open
  ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {
  
      }
  };
  
  /** Handles data for raw picture */
  PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
  
      }
  };
  
  /** Handles data for jpeg picture */
  PictureCallback jpegCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
          bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
  
          String root = Environment.getExternalStorageDirectory().toString();
          File myDir = new File(root + "/Easy_Measurement_images");
          myDir.mkdirs();
          Random generator = new Random();
          int n = 10000;
          n = generator.nextInt(n);
          String fname = "Image-" + n + ".jpg";
          File file = new File(myDir, fname);
          if (file.exists())
              file.delete();
          try {
              FileOutputStream out = new FileOutputStream(file);
              bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
              out.flush();
              out.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          Intent intent = new Intent(PhotoPreview.this,
                  VerticalAdjustmentActivity.class);
          startActivity(intent);
      }
  };
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
          int height) {
  
      // Set camera preview size,orientation,rotation using parameters
      if (camera != null) {
          Camera.Parameters parameters = camera.getParameters();
          parameters.set("orientation", "portrait");
          camera.setParameters(parameters);
          camera.startPreview();
      }
  
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  
      System.out.println("CAM ID %%%%%%%%%%%%%%$$$$$$$$$$$$$$$$$$ "+camId);
      camera = Camera.open(camId);
  
      if (camera != null) {
          try {
              camera.setPreviewDisplay(holder);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
  
      System.out.println("Surface destroyed ***************");
      if (camera != null) {
          camera.stopPreview();
          camera.release();
          camera = null;
      }
  
  }
  </code></pre>
  
  <p>}</p>