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?
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
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);
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" />
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.
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);
}