Camera not working on work profile - Xamarin Android

640 views Asked by At

I've developed an app using Xamarin for android devices. I am using the media picker from the Xamarin.Essentials package. In my place of work we have two types of devices - one has a work profile side, that uses Microsoft intunes. The application runs fine of the other devices but crashes when I open the camera on the device with the work profile. I have asked for the relevant permissions in the manifest. Does anyone have any experience with this? This is my first time using Xamarin. I've included part of my code below and the error it throws.

Kind Regards

bool x = MediaPicker.IsCaptureSupported;
            if(x == false)
            {
               await DisplayAlert("ok", "alert", "cancel");
            }
            var photo = await MediaPicker.CapturePhotoAsync();

            var stream = await photo.OpenReadAsync();

            newImage.Source = ImageSource.FromStream(() => stream);
    
     

      java.lang.RuntimeException
java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3827)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4003)
  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2317)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8595)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
Caused by: java.lang.IllegalArgumentException: 
  at androidx.core.content.FileProvider.parsePathStrategy (FileProvider.java:606)
  at androidx.core.content.FileProvider.getPathStrategy (FileProvider.java:579)
  at androidx.core.content.FileProvider.getUriForFile (FileProvider.java:417)
  at crc64a0e0a82d0db9a07d.IntermediateActivity.n_onCreate (Native Method)
  at crc64a0e0a82d0db9a07d.IntermediateActivity.onCreate (IntermediateActivity.java:31)
  at android.app.Activity.performCreate (Activity.java:8207)
  at android.app.Activity.performCreate (Activity.java:8191)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3800)

UPDATE: I think I may need to request permissions for the app to access the camera that is on the work profile? Is this possible

1

There are 1 answers

2
FreakyAli On

After reading the docs I realised the issue is probably because of the thread.

When you read the docs it clearly says:

All methods must be called on the UI thread because permission checks and requests are automatically handled by Xamarin.Essentials.

So the code you are using to get the photo should look something like below :

 var photo = await Xamarin.Essentials.MainThread.
            InvokeOnMainThreadAsync(
            async () => await MediaPicker.CapturePhotoAsync());

This means your full code would look like this:

bool x = MediaPicker.IsCaptureSupported;
        if(x == false)
        {
           await DisplayAlert("ok", "alert", "cancel");
        }
       var photo = await Xamarin.Essentials.MainThread.
            InvokeOnMainThreadAsync(
            async () => await MediaPicker.CapturePhotoAsync());
        var stream = await photo.OpenReadAsync();

        newImage.Source = ImageSource.FromStream(() => stream);

Also it's kinda weird this code works on your other device!