简体   繁体   中英

Xamarin WebView : Why Camera shows black screen on physical device?

I have a Xamarin Forms app that opens an html page in WebView.

The page is supposed to show the device camera.

I achieving this using an Android Custom Renderer as follows:

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));
        Control.Settings.BuiltInZoomControls = true;
        Control.Settings.SetSupportZoom(true);            
        Control.ScrollbarFadingEnabled = false;
    }
    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());});
        }
    }

The app targets Android 10.

The app and the camera work properly on the emulator. However on a physical device running Android 11, the camera shows a black screen.

Does anyone know why the cameda doesn't work on a pysical device please?

Thanks. Regards,

The camera shows black screen always because it doesn't have the permission. If it doesn't show a dialog to make the user to grant the permission when you run it in the physical device means it doesn't have the permission so that you could use the ContextCompat.checkSelfPermission() method or do Runtime permission check to check the permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM