Front Camera should open as Default, But it works only few devices, Android studio

153 Views Asked by At

I'm trying to add click image of user from Android mobile app in Android studio, and want open front camera always instead of back camera. But it only works on some devices and I want it to work with every device. Currently I'm using an OnePlus, which doesn't open the front camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

I've tried this

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT); startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
1

There are 1 best solutions below

0
Jaspalsinh Gohil On

The constant Camera.CameraInfo.CAMERA_FACING_FRONT was deprecated in Android API 23. You should use the constant CameraCharacteristics.LENS_FACING_FRONT instead.

Try adding the following extra to the intent:

intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);

You can also use CameraX library to open the front camera:

val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
  val cameraProvider = cameraProviderFuture.get()
  val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build()
  val camera = cameraProvider.bindToLifecycle(this, cameraSelector)

  // Start the camera preview
  camera.preview.setSurfaceProvider(viewFinder.surfaceProvider)
  camera.startPreview()
}) { e -> e.printStackTrace() }