38

I'm using an intent to open the camera with the native application:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    Uri photoUri = Uri.fromFile(getOutputPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

    startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE);

Every time the camera that is opened (front/back camera) is like the last time this native camera application was open. Meaning that if the last time I closed the native camera application the back camera was active, so when I launch the intent for camera, the back camera will be active.

I want to launch directly the front camera with the intent. Does anybody know how to do that?

Yaniv
  • 3,321
  • 8
  • 30
  • 45

8 Answers8

36

Word of caution: its a hack

Add this to the intent

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

This solution isn't sustainable, its using a testing code of the Camera app. For more info look at the "getCameraFacingIntentExtras" static method in Util class of the AOSP Camera project.

Update: Looks like that it was disabled in L

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
user213493
  • 892
  • 8
  • 10
19

Taken from Google Camera's shortcut for Android 7.1 (but should work with older Androids)

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

So, combined with previous answers, this works for me on all phones I could've test it on

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);
Pitel
  • 5,334
  • 7
  • 45
  • 72
9

Following code worked for most of the devices that were important to me:

 val targetPackage = getMediaCaptureIntent(imageUri).resolveActivity(packageManager)

Photo Camera:

fun getMediaCaptureIntent(mediaUri: Uri, targetPackage: String? = null): Intent {
    return Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
        putExtra(MediaStore.EXTRA_OUTPUT, mediaUri)

        // Extras for displaying the front camera on most devices
        putExtra("com.google.assistant.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extras.LENS_FACING_FRONT", 1)
        putExtra("android.intent.extras.CAMERA_FACING", 1)

        // Extras for displaying the front camera on Samsung
        putExtra("camerafacing", "front")
        putExtra("previous_mode", "Selfie")

        if (targetPackage?.contains("honor", ignoreCase = true) == true) {
            // Extras for displaying the front camera on Honor
            putExtra("default_camera", "1")
            putExtra("default_mode", "com.hihonor.camera2.mode.photo.PhotoMode")
        } else {
            // Extras for displaying the front camera on Huawei
            putExtra("default_camera", "1")
            putExtra("default_mode", "com.huawei.camera2.mode.photo.PhotoMode")
        }
    }
}

Video Camera:

fun getMediaCaptureIntent(mediaUri: Uri, targetPackage: String? = null): Intent {
    return Intent(MediaStore.ACTION_VIDEO_CAPTURE).apply {
        putExtra(MediaStore.EXTRA_OUTPUT, mediaUri)

        // Extras for displaying the front camera on most devices
        putExtra("com.google.assistant.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
        putExtra("android.intent.extras.LENS_FACING_FRONT", 1)
        putExtra("android.intent.extras.CAMERA_FACING", 1)

        // Extras for displaying the front camera on Samsung
        putExtra("camerafacing", "front")
        putExtra("previous_mode", "Video")

        if (targetPackage?.contains("honor", ignoreCase = true) == true) {
            // Extras for displaying the front camera on Honor
            putExtra("default_camera", "1")
            putExtra("default_mode", "com.hihonor.camera2.mode.video.VideoMode")
        } else {
            // Extras for displaying the front camera on Huawei
            putExtra("default_camera", "1")
            putExtra("default_mode", "com.huawei.camera2.mode.video.VideoMode")
        }
    }
}
Evgeny
  • 431
  • 3
  • 10
  • 1
    unfortunately for Samsung it does turn to the front camera, but with the PICTURE mode, and not with the VIDEO mode as expected (No, I did not copy the entire code, just the line with Samsung comments). Any idea what has gone awry? – Hrvoje Sep 09 '21 at 23:16
4
    pictureIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
    pictureIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
    pictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
working on intex 
Amar Singh
  • 435
  • 5
  • 9
3

There's no intent (AFAIK) that specifically targets the front-facing camera.

To do it programmatically: Android SDK <= 2.2 only supports use of a single camera (the first back-facing camera). For 2.3+, you can loop thru the cameras and figure out which is the front facing one (if available). It'd be something like...

Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
    CameraInfo camInfo = new CameraInfo();
    Camera.getCameraInfo(camNo, camInfo);
    if (camInfo.facing.equals(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
        cam = Camera.open(camNo);
    }
}
if (cam == null) {
   // no front-facing camera, use the first back-facing camera instead.
   // you may instead wish to inform the user of an error here...
   cam = Camera.open();
}
// ... do stuff with Camera cam ...

This example is only skeletal and doesn't provide any (much needed) error handling.

EDIT: You also need to add these to your manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
2

The following code will work until Android 11 including Samsung phone

fun updateIntentForCameraFacing(cameraIntent: Intent, frontFacing: Boolean){
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
        if(frontFacing)
            cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_BACK)
        else
            cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)

    }
    else if(frontFacing){
        cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_BACK)
        cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)

        //samsung
        cameraIntent.putExtra("camerafacing", "front")
        cameraIntent.putExtra("previous_mode", "front")
    }
    else{
        cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)
        cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", false)

        //samsung
        cameraIntent.putExtra("camerafacing", "rear")
        cameraIntent.putExtra("previous_mode", "rear")
    }
}
0

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
  intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
} else {
  intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
-1

Have you tried watching adb logcat while switching to the front camera in your native camera application? If it is indeed another activity, then it will show up as such there and you can simply copy the intent to your application. If it does not show up, you are out of luck, I guess.

devsnd
  • 7,382
  • 3
  • 42
  • 50