11

I want to make a app in which I have to open only front camera, How can i do it using intent?

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

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

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
Nikhil
  • 3,711
  • 8
  • 32
  • 43
Abhishek Joshi
  • 361
  • 1
  • 7
  • 17

3 Answers3

16

java

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri = Uri.fromFile(getOutputPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE);

Other/Alternate Solution

private Camera openFrontFacingCameraGingerbread() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.toString());
            }
        }
    }

    return cam;
}

add these permissions in the AndroidManifest.xml file

<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" />

only available in Gingerbread(2.3) and Up Android Version.

otherwise you can also check these example

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

hope it helps you..

skydroid
  • 733
  • 6
  • 16
  • 1
    I used camera API to open front camera but after capturing image it save image after rotating 90 degree clock wise. How can i save image correctly. – Abhishek Joshi Sep 10 '16 at 13:05
  • may be this [**answer**](http://stackoverflow.com/a/12933632/6617272) will help you – skydroid Sep 12 '16 at 05:27
4

Note: you're trying to use Intent correctly (and also note @skydroid answer below). However, it doesn't work for you probably because of below:

Standard Android Camera

Using Intent you only open the standard android camera application (back camera is default there).

Never use android.intent.extras.CAMERA_FACING property - this is an undocumented feature that stopped working starting from some of the android versions.

Camera API

To open a front camera you should use Camera API - do things like choosing the front camera, showing a preview in a view, and taking pictures manually. Answer of @skydroid shows how to find the front camera. Note that Camera.open() doesn't open a camera for the user as you might expect, you should manually show the preview.

Also note, since API level 21 the Camera API is deprecated and docs recommend to use Camera 2 API instead. But Camera API remains fully functional and you have no other choice if you want support older versions (< API level 21) as well.

Artem Novikov
  • 4,087
  • 1
  • 27
  • 33
  • 2
    And the question remains: "How can I open the front camera using Intent in android?" ???? – ekashking Oct 16 '21 at 20:40
  • @ekashking Both author and "skydroid" below already show correct usage of Intent to do that. I only noted that using an Intent for front camera "is an undocumented feature that stopped working starting from some of the android versions" therefore it's not good idea to use it (and probably that's why it doesn't work for author). As an alternative, I mentioned Camera API is preferred option (in case author wasn't aware) and pointed to "skydroid" answer, because he already described Camera API (as well as Intent) usage nicely. P.S. Note, this answer is 5 years old, things might have changed now. – Artem Novikov Mar 17 '22 at 15:28
3
try this:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
                File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Util.SD_CARD_PATH);
                if (!outPutFile.exists()) {
                    outPutFile.mkdirs();
                }
                capturedImageUri = Uri.fromFile(File.createTempFile("packagename" + System.currentTimeMillis(), ".jpg", outPutFile));

                intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                startActivityForResult(intent, Util.REQUEST_CAMERA);
Jinal Patel
  • 699
  • 5
  • 15