How to launch front camera with intent?
Asked Answered
W

8

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?

Waxwing answered 13/3, 2012 at 14:37 Comment(4)
this link might be help full #2779502Enure
It is done there without an intent. Thanks anywayWaxwing
@Yaniv,Did you got the solution for this?Custody
did you find any solution for this one?Inhospitable
I
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

Inchoation answered 22/6, 2012 at 15:52 Comment(8)
I tried this but it does not worked, can you hellp me ??Khadijahkhai
Doesn't work on Google Camera, Nexus 4, Android 5.0 Lollipop.Photocopy
can we only open the back camera through intent. I mean disable fron cameraVelazquez
Actually, 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); } Working on a Nexus 5 5.1 (if case) and a Samsung S4 5.0.1 (else case), but not on Honor 7...Lathrope
tried all in my nexus 5 with marsh, it always open back camera.Highroad
Tried in samsung devices, always showing back cameraSciatic
@Sciatic have you find any solution for opening front camera in samsung ?Tamberg
No @Tamberg As far I know, we don't have option to open front camera with default camera Intent, to achieve we need to implement custom cameraSciatic
K
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);
Kreg answered 21/10, 2016 at 11:26 Comment(0)
D
10

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")
        }
    }
}
Douala answered 27/4, 2021 at 17:41 Comment(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?Ignatz
H
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 
Hyaline answered 21/6, 2019 at 11:25 Comment(0)
A
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" />
Amply answered 13/3, 2012 at 16:18 Comment(2)
I know this code as you wrote. But I have to that with an Intent. Thanks anyway.Waxwing
Am getting " Camera failed to open : Fail to connect to camera service" in a samsung device(android version 4.04).Rule
S
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")
    }
}
Stollings answered 2/8, 2021 at 18:10 Comment(0)
E
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);
}
Engdahl answered 11/10, 2016 at 10:26 Comment(0)
E
-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.

Equities answered 13/3, 2012 at 16:5 Comment(1)
There is nothing relevant on the logcat. Thanks anyway.Waxwing

© 2022 - 2024 — McMap. All rights reserved.