UnauthorizedAccessException when denying camera access
See original GitHub issueHello,
I’m using ZXingScannerView in my app. When I push the button which opens the page containing that control a popup appears asking for camera permissions (Camera permissions are set in the visual studio project file for the android app).
When I accept the permissions everything runs fine, but when I deny it the app crashes with an UnauthorizedAccessException.
On the page I simply added the control:
<qr:ZXingScannerView x:Name="ScannerView" HorizontalOptions="FillAndExpand" OnScanResult="HandleScannerOnResultEvent" VerticalOptions="FillAndExpand" />
In the MainActivity.OnCreate I’m doing the following:
Platform.Init(); MobileBarcodeScanner.Initialize(Application);
and also in the MainActivity:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults); }
The last lines of the call stack are:
01-09 11:15:58.521 D/ZXing.Net.Mobile( 3973): System.NullReferenceException: Object reference not set to an instance of an object.
01-09 11:15:58.521 D/ZXing.Net.Mobile( 3973): at ZXing.Mobile.CameraAccess.CameraController.RefreshCamera () [0x0001e] in <819b29aa6d91462699e19a679be55a44>:0
I’m using ZXing v2.4.1. Android: several phones and tablets running Android version 6.0.1 and 8.0
Have I missed something in my implementation? Since the ScannerView control forces the popup and waits for the result as well I’m expecting that it will be handled there. Or do I have to do the check in advance before instantiating the control? But the projects main page says that ZXing handles the permission request for me.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:7 (1 by maintainers)

Top Related StackOverflow Question
3.0.0 changes this behaviour. Now just a black screen appears instead of the exception.
Same. Even using the samples provided in this repository causes the
UnauthorizedAccessExceptionto be thrown when denying access to the camera.After further investigating this behaviour my colleagues and I found that this line in the
PermissionHandler.csis the cause. The message is kinda misleading in that it always says that permission was not found in theAndroidManifest.xmleven though it is and the actual reason is simply the User denying access.Changing:
to
Fixes the issue for our use case. Mind you the application still crashes when the camera permission is not present in the manifest.xml.
But the behaviour after changing the if statement is the following: User opens the ScanView or Activity and gets asked for the camera permission. If the User denies than the screen will be black/white whatever. If the User navigates back and forth will be asked again until the User accepts or checks ‘dont ask again’ and denies.