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

13,451

Solution 1

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

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

Solution 3

Try this for open the back camera

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE );

Solution 4

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>

Solution 5

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);
            }
        });
Share:
13,451
RAJAN
Author by

RAJAN

I wants to live simple and learn new thing

Updated on June 04, 2022

Comments

  • RAJAN
    RAJAN almost 2 years

    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
    RAJAN about 7 years
    Here i can open front camera but how to see the recent taken picture and confirm then only it's going to save in device else remove and give retake option to take picture.
  • Gowtham Subramaniam
    Gowtham Subramaniam about 7 years
    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)
  • priyanka kamthe
    priyanka kamthe about 7 years
  • priyanka kamthe
    priyanka kamthe about 7 years
    Most Welcome! If this answer is really works for you then make its as acceptable!
  • Yasiru Nayanajith
    Yasiru Nayanajith over 5 years
    Its not official
  • sandeepmaaram
    sandeepmaaram over 4 years
    It is not working. Tested samsung devices and OS 9!!
  • Dharmishtha
    Dharmishtha over 3 years
    This does not opens front camera in Samsung M01 having android 10. any solution ?