Failed to connect to camera service

前端 未结 10 1936
梦毁少年i
梦毁少年i 2020-11-28 10:13

I\'m trying to access the camera on my phone. I\'m writing a simple stub app prior to putting the code in a widget. I\'m not getting very far. The code always throws a runti

相关标签:
10条回答
  • 2020-11-28 10:39

    Make sure to call the release() method to release the camera when it is no longer needed, or you will not be able to use the camera. Perhaps as a sanity check, see if your regular camera works. If it says it fails, then your previous attempts at runni

    0 讨论(0)
  • 2020-11-28 10:41

    I came up with the same problem and I'm sharing how I fixed it. It may help some people.

    First, check your Android version. If it is running on Android 6.0 and higher (API level 23+), then you need to :

    1. Declare a permission in the app manifest. Make sure to insert the permission above the application tag.

    **<uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />**
    
    <application ...>
        ...
    </application>
    

    1. Then, request that the user approve each permission at runtime

        if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA)
          != PackageManager.PERMISSION_GRANTED) {
      // here, Permission is not granted
      ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.CAMERA}, 50);
      }
      

    For more information, have a look at the API documentation here

    0 讨论(0)
  • 2020-11-28 10:47

    running your code hundred times may affect the camera to function wrongly.Your activity may be performing correctly but system could not buy it.so camera forces stop. One main tip all missed is rebooting your phone and not only eclipse..It worked for me..

    0 讨论(0)
  • 2020-11-28 10:51

    since this question was asked 4 years back..and i didn't realised that unless mentioned by the Questioner..when there were no Run time permissions support.

    but hoping it useful for the users who still caught in this situation.. Have a look at Run Time Permissions ,for me it solved the problem when i added Run time permissions to grant camera access. Alternatively you can grant permissions to the app manually by going to your mobile settings=>Apps=>(select your app)=>Permissions section in the appeared window and enable/disable desired permissions. hope this will work.

    0 讨论(0)
  • 2020-11-28 10:54

    The problem is related to permission. Copy following code into onCreate() method. The issue will get solved.

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

    After that wait for the user action and handle his decision.

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CAMERA_PERMISSION_REQUEST_CODE:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //Start your camera handling here
                } else {
                    AppUtils.showUserMessage("You declined to allow the app to access your camera", this);
                }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 10:58

    when you use camera.open; and you finish using the camera write this commend camera.release(); this will stop the camera so you can use it again

    0 讨论(0)
提交回复
热议问题