RAJAN
RAJAN

Reputation: 146

How to Open Front and Back camera through intent in android?

We implement camera with intent it's working fine in micromax480p version 5.1 but when we used in Nexus7 version 6.1 by that time camera is opened but i want to open some times Front and somr times Back is it possible to open according to as we need.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //intent.addCategory(Intent.CATEGORY_OPENABLE);
    //intent.setAction(Intent.ACTION_GET_CONTENT);
    if(camera == 1) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            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);

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


    // start the image capture Intent
    startActivityForResult(intent, requestCode);

I used like this but it's open defaultly what ever we used camera(front and back) previously.thanks. sorry for my weak English comm.

Upvotes: 5

Views: 19403

Answers (6)

Shubham Raitka
Shubham Raitka

Reputation: 1052

To open the back camera:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

To open the front camera:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
when {
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)  // Tested on API 24 Android version 7.0(Samsung S6)
     }
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 27 Android version 8.0(Nexus 6P)
         cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
     }
     else -> cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1)  // Tested API 21 Android version 5.0.1(Samsung S4)
}
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

I could not make it work for API 28 and above. Also, opening the front camera directly is not possible in some devices(depends on the manufacturer).

Upvotes: 4

ibrahimyanik
ibrahimyanik

Reputation: 332

Try this for open the back camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE );

Upvotes: 1

Programador
Programador

Reputation: 133

This work for me

btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    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);
                } else {
                    intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
                }

                Uri fileUri = // your fileUri

                if(fileUri == null) {
                    Toasty.error(MainActivity.this, "Erro diretorio", Toast.LENGTH_SHORT, true).show();
                } else {

                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                }
                startActivityForResult(intent, 100);
            }
        });

Upvotes: 1

Jordon
Jordon

Reputation: 191

Try the code below

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); 
}

Upvotes: 2

Gowtham Subramaniam
Gowtham Subramaniam

Reputation: 3478

To call the Front camera you can use:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

To call the Back camera you can use:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);

And you need to set the permission for the camera in your AndroidManifest.xml:

 <uses-permission android:name="android.permission.CAMERA"> </uses-permission>

Upvotes: 1

priyanka kamthe
priyanka kamthe

Reputation: 529

Android SDK <= 2.2 only supports use of a single camera (the first back-facing camera). For 2.3+, you can loop through 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 ...

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

Upvotes: 0

Related Questions