打开前置摄像头以通过相机意图进行自拍

问题描述

我想打开前置摄像头,以便用户可以点击自拍,但无论我尝试过什么,前置摄像头都不会自动打开。

以下是我试过的代码。我尝试了不同的组合,但仍然无法让应用程序打开前置摄像头。在 Android 8、9、10 和 11 上试过

Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(this,this.getApplicationContext().getPackageName() + ".provider",destination);
            CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
        } else {
            uri = Uri.fromFile(destination);
            CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
        }
        CamIntent.putExtra("return-data",true);
       CamIntent.putExtra("android.intent.extras.CAMERA_FACING",android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
        CamIntent.putExtra("android.intent.extras.LENS_FACING_FRONT",1);
        CamIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA",true);
        CamIntent.putExtra("android.intent.extras.CAMERA_FACING",1);

        startActivityForResult(CamIntent,10);
openFrontFacingCameraGingerbread();

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("camera runtimeexception",e.toString());

                }
            }
        }

        return cam;
    }

我有华硕 Zenfone 6 旋转相机手机 openFrontFacingCameraGingerbread() 方法尝试翻转打开前置摄像头,但突然停止。日志收到消息“错误”。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)