0

我正在为大多数版本的 Android 重新编写应用程序。该应用程序设置为android.hardware.Camera.Open()在启动时调用。当应用程序启动时,它会给出错误Camera Error: Could not connect to camera

中的权限AndroidManifest.xml

<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.camera"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>`.

为什么连接不上摄像头?

4

4 回答 4

2

问题解决了!

这个问题只是大小写的问题。该行<uses-permission android:name="android.permission.camera"/>实际上应该是<uses-permission android:name="android.permission.CAMERA"/>. 发生这种情况是因为camera是硬件功能,CAMERA而是许可。

于 2016-12-29T14:20:36.053 回答
1

不仅要声明,manifest而且您需要permission在运行时从用户授予Android 6.+ 使用以下代码片段进行授予camera permission

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            if (ContextCompat.checkSelfPermission(SplashScreenActivty.this, Manifest.permission.CAMERA)
                                    != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions(this,
                                        new String[]{Manifest.permission.CAMERA},
                                        1);
                            } 

                        }


 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case 1: {

                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                    // permission was granted, yay! Do the
                    // camera-related task you need to do.
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

                    Toast.makeText(this, "Permission denied to read SMS", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
于 2016-12-29T07:14:26.277 回答
0

在之前的 android 版本中,为了使用摄像头硬件,足以在 android manifest 中声明适当的权限和用途,但由于出现了 Marshmallow 版本,因此不再足以运行该应用程序。因为还需要启用运行时权限。考虑使用类似的代码,并使其适合您的项目,因为它使您有机会检查和启用权限。

  @Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions, int[] grantResults) {
    if (requestCode == PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            cameraPreview.initializeCamera();
        } else {

        }
        return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PERMISSION_REQUEST_CODE) {
        cameraPreview.initializeCamera();
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


   public void requestCameraPermission() {
    ActivityCompat.requestPermissions(this,
            new String[]{
                    Manifest.permission.CAMERA
            },
            PERMISSION_REQUEST_CODE);
}

public void requestPermissionWithRationale() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        Snackbar.make(findViewById(android.R.id.content), R.string.no_cam_permission, Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.snackbar_action_grant, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestCameraPermission();
                    }
                })
                .show();
    } else {
        requestCameraPermission();
    }
}
于 2016-12-29T07:20:02.197 回答
0

将相机用于已弃用

下面是代码

更新--->

  @Module
public class CameraModule {

    @Provides
    CameraSupport provideCameraSupport() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return new CameraNew(context);
        } else {
            return new CameraOld();
        }
    }
}

然后对于旧目标:

 @SuppressWarnings("deprecation")
public class CameraOld implements CameraSupport {

    private Camera camera;

    @Override
    public CameraSupport open(final int cameraId) {
        this.camera = Camera.open(cameraId);
        return this;
    }

    @Override
    public int getOrientation(final int cameraId) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        return info.orientation;
    }
}

对于新目标:

  public class CameraNew implements CameraSupport {

    private CameraDevice camera;
    private CameraManager manager;

    public CameraNew(final Context context) {
        this.manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public CameraSupport open(final int cameraId) {
        try {
            String[] cameraIds = manager.getCameraIdList();
            if (ActivityCompat.checkSelfPermission(CameraPreview.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.

                return null;
            }
            manager.openCamera(cameraIds[cameraId], new CameraDevice.StateCallback() {
                @Override
                public void onOpened(CameraDevice camera) {
                    CameraNew.this.camera = camera;
                }

                @Override
                public void onDisconnected(CameraDevice camera) {
                    CameraNew.this.camera = camera;
                    // TODO handle
                }

                @Override
                public void onError(CameraDevice camera, int error) {
                    CameraNew.this.camera = camera;
                    // TODO handle
                }
            }, null);
        } catch (Exception e) {
            // TODO handle
        }
        return this;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public int getOrientation(final int cameraId) {
        try {
            String[] cameraIds = manager.getCameraIdList();
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraIds[cameraId]);
            return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        } catch (CameraAccessException e) {
            // TODO handle
            return 0;
        }
    }
}
于 2016-12-29T07:28:08.697 回答