0

I have a web page I'm loading in Xamarin WebView.

The page is supposed to open the device camera. The page works properly on my laptop and shows the camera image. However the same page doesn't show the camera when used in a WebView in Xamarin Android. The camera access is granted.

Here is my code :

I have a custom webrenderer as follows :

[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebView))]
namespace App45.Droid
{
    public class MyWebViewRenderer : WebViewRenderer
    {
        Activity mContext;
        public MyWebViewRenderer(Context context) : base(context)
        {
            this.mContext = context as Activity;
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            Control.Settings.JavaScriptEnabled = true;
            Control.ClearCache(true);
            Control.SetWebChromeClient(new MyWebClient(mContext));
        }
        public class MyWebClient : WebChromeClient
        {
            Activity mContext;
            public MyWebClient(Activity context)
            {
                this.mContext = context;
            }
            [TargetApi(Value = 21)]
            public override void OnPermissionRequest(PermissionRequest request)
            {
                mContext.RunOnUiThread(() => {
                    request.Grant(request.GetResources());

                });

            }
        }

    }

}

This is my MainActivity.cs:

namespace SS.Droid
{
    [Activity(Label = "Soirée Sympa", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
         

        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
       
    }
} 

Android Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="5" android:versionName="5.1" package="somthing.app" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
    <application android:label="MyApp" android:icon="@mipmap/launcher_foreground"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
</manifest>

No error is raised and the camera is not shown either in the emulator nor in a physical device :

enter image description here

Does anyone have a solution to this please ?

Thanks, Cheers

Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126

1 Answers1

0

I have done this before. You could request the permission in OnAppearing() method. The way i used to request the permission in the link is plugin. I rechecked the code i provided with source below to check camera. It still works.

https://test.webrtc.org/

The thread i done before: Xamarin - Requesting camera permissions in WebView

For now, you could use Xamarin.Essentials for Permissions. https://learn.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17