Failed to connect to camera service

前端 未结 10 2044
梦毁少年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: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.

    **
    **
    
    
        ...
    
    

    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

提交回复
热议问题