How do I open the “front camera” on the Android platform?

前端 未结 9 2348
挽巷
挽巷 2020-11-22 15:00

More generally, if a device has more than one embedded camera, is there a way to initialize one of them in particular?

I didn\'t find it in Android reference documen

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 16:02

    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).

提交回复
热议问题