How do I check that the user has allowed camera access in a UWP app?

筅森魡賤 提交于 2019-12-13 03:27:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!