问题
How do I programmatically check if an UWP app has camera access? I'm trying to do an if
check to display some help text if the user has denied access to the camera.
I'm trying to get access to it in Unity through Application.HasUserAuthorization, but that doesn't seem to give the right result (eg, it returns true
even when you deny access)
回答1:
How do I programmatically check if an UWP app has camera access?
An UnauthorizedAccessException
will be thrown when you attempt to initialize the camera if the user has disabled camera access in the device's privacy settings. You will also see this exception during development if you have neglected to add the proper capabilities to your app manifest. So that you could create a new instance of the MediaCapture
class and call InitializeAsync
to initialize the capture device. This method may fail, on devices that don't have a camera for example, so you should call it from within a try block.
// Initialize MediaCapture
try
{
await _mediaCapture.InitializeAsync(settings);
_isInitialized = true;
}
catch (UnauthorizedAccessException ex)
{
Debug.WriteLine("The app was denied access to the camera");
}
Details please check this section.
来源:https://stackoverflow.com/questions/49861675/how-do-i-check-that-the-user-has-allowed-camera-access-in-a-uwp-app