3

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.

RAJAN
  • 126
  • 1
  • 1
  • 12
  • I used also camera API through Surfaceview but what will happen when we open camera and take the picture after that i can not see the preview whether this image is right or not i.e. user confirmation.because of that i used camera app and when user take picture after that they confirm then only we will save else reject it. – RAJAN May 08 '17 at 07:14
  • as @gowtham stated use the nested `if` to wrap them based on your requirement.You also use `switch`. – sanjeev May 25 '18 at 07:07

6 Answers6

4

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).

Shubham Raitka
  • 1,034
  • 8
  • 15
  • 1
    This does not opens front camera in Samsung M01 having android 10. any solution ? – Dharmishtha Feb 05 '21 at 07:51
  • 1
    it's not working for me. i'm trying to set back camera as default and open only this way but unsuccessfully. my device opens last-used camera for some reason – Kirguduck Jul 20 '23 at 11:45
2

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); 
}
Pado
  • 1,497
  • 16
  • 28
Jordon
  • 191
  • 1
  • 12
1

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>
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
  • Use Condition, 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) – Gowtham Subramaniam May 08 '17 at 08:46
  • Its not official – Yasiru Nayanajith Aug 23 '18 at 14:28
  • 1
    It is not working. Tested samsung devices and OS 9!! – sandeepmaaram Nov 27 '19 at 13:47
  • for front camera you need to use intent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)//LENS_FACING_FRONT = 0 – Kirguduck Jul 17 '23 at 14:52
1

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);
            }
        });
Programador
  • 133
  • 1
  • 5
1

Try this for open the back camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE );
ibyanik
  • 302
  • 1
  • 4
  • 18
0

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" />
priyanka kamthe
  • 519
  • 2
  • 5
  • 19