Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch front camera with intent?

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?

like image 294
Yaniv Avatar asked Mar 13 '12 14:03

Yaniv


People also ask

How do you open a Camera with intent?

Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use onActivityResult() method to get the result, here the captured image.

What is Camera intent?

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data" . The following code retrieves this image and displays it in an ImageView .

What is Camera intent android?

Intent. By the help of 2 constants of MediaStore class, we can capture picture and video without using the instance of Camera class. ACTION_IMAGE_CAPTURE.


2 Answers

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

like image 64
user213493 Avatar answered Oct 07 '22 11:10

user213493


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); 
like image 21
Pitel Avatar answered Oct 07 '22 12:10

Pitel