简体   繁体   English

Xamarin WebView:为什么相机在物理设备上显示黑屏?

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

I have a Xamarin Forms app that opens an html page in WebView.我有一个 Xamarin Forms 应用程序,它在 Z3E7D16622A52391BD7BF68CAE 中打开一个 html 页面

The page is supposed to show the device camera.该页面应该显示设备相机。

I achieving this using an Android Custom Renderer as follows:我使用 Android 自定义渲染器实现了这一点,如下所示:

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.该应用程序针对 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.然而,在运行 Android 11 的物理设备上,摄像头显示黑屏。

Does anyone know why the cameda doesn't work on a pysical device please?有谁知道为什么cameda不能在物理设备上工作?

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.如果在物理设备中运行它时没有显示让用户授予权限的对话框,则意味着它没有权限,因此您可以使用 ContextCompat.checkSelfPermission() 方法或执行运行时权限检查检查权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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