java.lang.RuntimeException: Fail to Connect to camera service

后端 未结 4 1664
执笔经年
执笔经年 2020-12-06 18:02

I tried to make an app than can switch my camera flash on and off. The code I have atm looks like this:

Camera flash;
Camera.Parameters params;

flash = Came         


        
相关标签:
4条回答
  • 2020-12-06 18:19

    Usually that problem is due to the missing camera request permission, as already said by other users.

    But, just to register here another cause, if you try to open the camera using a cameraID that does not exist, you will receive that same error

    java.lang.RuntimeException: Fail to Connect to camera service

    0 讨论(0)
  • 2020-12-06 18:29

    To access the device camera, you must declare the CAMERA permission in your Android Manifest like this,

     <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" />
    
    0 讨论(0)
  • 2020-12-06 18:29

    You need to add the new request permission on android 6.x programmatically before.

     private static final int MY_PERMISSIONS_REQUEST_CAMERA = 555;
    
    if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
            } else {
                IntentIntegrator.forSupportFragment(this).initiateScan();
            }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CAMERA: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    IntentIntegrator.forSupportFragment(this).initiateScan();
                } else {
                    FragmentHelper.popFragment(getActivity(), null, null);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 18:32

    You might have forgotten to call release in onDestroy

    For example:

    @Override
    protected void onDestroy() {
        if (mCamera != null) {
            mCamera.release();
        }
        super.onDestroy();
    }
    
    0 讨论(0)
提交回复
热议问题