1

I am creating an app, in which I am setting a profile image and cover image ,so on setting profile image I want to open the front camera by default using intent.
I am using

pictureIntent.putExtra("android.intent.extras.CAMERA_FACING",1);

Its working on Sony but when I tested on a Samsung Galaxy J4 ,it's opening the back camera. I searched and found somewhere that for Samsung one should use value 2. However it's not working.
I want to do this using intent only.
Does anyone have an idea about it?

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
Kittu
  • 11
  • 3

1 Answers1

2

You can do like below,

private Camera openFrontFacingCamera()  {
   int cameraCount = 0;
  Camera cam = null;
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
  cameraCount = Camera.getNumberOfCameras();
   for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
      Camera.getCameraInfo( camIdx, cameraInfo );
      if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
        try {
            cam = Camera.open( camIdx );
        } catch (RuntimeException e) {
            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
        }
    }
}

return cam;
}

And then use it in your app as follows:

public static Camera getCameraInstance() {
  Camera c = null;
  try {
    c = openFrontFacingCamera();
  }
  catch (Exception e){
 }
return c; 

}
IamVariable
  • 408
  • 6
  • 23
  • After adding this,getting below error in logcat. Camera_APM :: [APM]APM's Thread is started Camera_APM :: [APM] This app is forground app Camera: Error 2 – Kittu Aug 02 '19 at 10:11