How can I open front camera using Intent in android?

后端 未结 3 1670
走了就别回头了
走了就别回头了 2020-12-02 00:22

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 =          


        
3条回答
  •  無奈伤痛
    2020-12-02 00:52

    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

    
    
    
    

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

提交回复
热议问题