How to Open Front and Back camera through intent in android?

android, android-camera-intent, camera

Solution

To open the back camera:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

To open the front camera:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
when {
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)  // Tested on API 24 Android version 7.0(Samsung S6)
     }
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 27 Android version 8.0(Nexus 6P)
         cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
     }
     else -> cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1)  // Tested API 21 Android version 5.0.1(Samsung S4)
}
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

I could not make it work for API 28 and above. Also, opening the front camera directly is not possible in some devices(depends on the manufacturer).

Problem

We implement camera with intent it's working fine in micromax480p version 5.1 but when we used in Nexus7 version 6.1 by that time camera is opened but i want to open some times Front and somr times Back is it possible to open according to as we need. ``` Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); //intent.addCategory(Intent.CATEGORY_OPENABLE); //intent.setAction(Intent.ACTION_GET_CONTENT); if(camera == 1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT); intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1); intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true); } else { intent.putExtra("android.intent.extras.CAMERA_FACING", 1); } } // start the image capture Intent startActivityForResult(intent, requestCode); ``` I used like this but it's open defaultly what ever we used camera(front and back) previously.thanks. sorry for my weak English comm.

Original source