1

My android application sometimes crashes randomly when the app is launched and when I check my analytics report, the exception I get is :

{third party tool} requires android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

PermissionsHandler.CheckPermissions (Android.Content.Context context, System.String permission, System.Boolean throwOnError)

PermissionsHandler.CheckCameraPermissions (Android.Content.Context context, System.Boolean throwOnError)CameraController.SetupCamera () CameraAnalyzer.SetupCamera ()

The tool I used for barcode doesn't even use camera service until user navigates to certain page. I just feel that probably at run time it is looking for camera permissions and sometimes the app manifest is not ready or doesn't load so the app assumes camera permissions are not included in the appmanifest even though they are.

My question is

  1. Does AndoridManifest right at the start of the application or Do I have to do some changes to the application to make sure it waits until manifest it ready with all permission settings?
  2. Does anyone faced similar error or can suggest any options to resolve ?

UPDATE: ZXing is a third party tool that needs camera service. But this thirty party API is being called in certain pages once the app launches and on these pages it requests for camera permission but app crashes at launch time with this exception: Exception:

PermissionsHandler.CheckPermissions (Android.Content.Context context, System.String permission, System.Boolean throwOnError)
System.UnauthorizedAccessException: ZXing.Net.Mobile requires: android.permission.CAMERA, but was not found in your AndroidManifest.xml file.

PermissionsHandler.CheckPermissions (Android.Content.Context context, System.String permission, System.Boolean throwOnError)

PermissionsHandler.CheckCameraPermissions (Android.Content.Context context, System.Boolean throwOnError)

CameraController.SetupCamera ()

CameraAnalyzer.SetupCamera ()

ZXingSurfaceView+<SurfaceCreated>d__3.MoveNext ()

ExceptionDispatchInfo.Throw ()

AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state)

SyncContext+<>c__DisplayClass2_0.<Post>b__0 ()

Thread+RunnableImplementor.Run ()

IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this
Community
  • 1
  • 1
TheDeveloper
  • 1,127
  • 1
  • 18
  • 55
  • 1
    What's minSDK of your app? – Rafaela Lourenço Apr 12 '18 at 20:23
  • 1
    How about adding runtime permission in your java code? – D. B. Apr 12 '18 at 20:24
  • What API levels is the app crashing on? Are you not requesting the proper runtime permissions for Marsh+? – SushiHangover Apr 12 '18 at 20:38
  • @SushiHangover App crashes mostly on API 24. I have all permissions selected on Android.manifest and uses request permission access before using camera. But the app crashes during app launch before even going to the pages where we request permission access – TheDeveloper Apr 12 '18 at 20:39
  • @RafaelaLourenço Minimum API 19 - Maximum API-24. – TheDeveloper Apr 12 '18 at 20:42
  • @D.B. I didn't knew about runtime permissions. I do request for permissions on the pages where camera is needed but this occurs at app launch even before going to that page. – TheDeveloper Apr 12 '18 at 20:47
  • @TheDeveloper Can you post the error you get? – D. B. Apr 12 '18 at 20:54
  • @D.B. Updated the post with exception – TheDeveloper Apr 12 '18 at 21:01
  • @TheDeveloper API23+ you need to request runtime permissions (this is along with the hard-coded manifest permissions) https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/ – SushiHangover Apr 12 '18 at 21:21
  • @SushiHangover I am requesting the permissions during the runtime when the user reaches the page where they are needed. Say app has 4 pages a-b-c-d and camera is used on d page. When the user goes to page 4, the camera permission is requested to the user. But app is crashing even before any page is displayed. – TheDeveloper Apr 13 '18 at 03:25

3 Answers3

2

As mentioned here CAMERA permission is a dangerous one, so it requires runtime permission. I suggest you write that code on your first activity that appears after launching the app to ask for that permission.

private void VerifyPermission(){
   int permissionWriteExternalStorage = ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA);

    if(permissionWriteExternalStorage != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(YourActivityName.this, STORAGE_PERMISSIONS, 1);
    }
}

Just call this method in onCreate:

VeriryPermission();

Bear in mind that the method I posted is a very simple one and can be upgraded later.

D. B.
  • 488
  • 1
  • 10
  • 20
2

I found the solution here : Application crashes when asking for permissions

This code in your OnRequestPermissionsResult:

  Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  for (int i = 0; i < permissions.Length; i++)
  {
    if (permissions[i].Equals("android.permission.CAMERA") && grantResults[i] == Permission.Granted)
      global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  }
  base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
1

First of all you need to add this in your AndroidManifest no matter what sdk level you support

<uses-permission android:name="android.permission.CAMERA" />

On top of that for API level 23 and above you need to explicitly request for user permission.

And to answer your first question: AndroidManifest is loaded automatically so you don't have to do anything special.

Hope this helps!!

Amey
  • 242
  • 3
  • 11