【问题标题】:Xamarin WebView : Why Camera shows black screen on physical device?Xamarin WebView:为什么相机在物理设备上显示黑屏?
【发布时间】:2021-11-20 11:59:04
【问题描述】:

我有一个 Xamarin Forms 应用程序,可以在 WebView 中打开一个 html 页面。

该页面应该显示设备摄像头。

我使用 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());});
        }
    }

该应用面向 Android 10。

应用程序和相机在模拟器上正常工作。但是在运行 Android 11 的物理设备上,摄像头显示黑屏。

请问有人知道为什么cameda不能在物理设备上工作吗?

谢谢。 问候,

【问题讨论】:

    标签: android xamarin


    【解决方案1】:

    相机总是黑屏,因为它没有权限。如果在物理设备中运行它时没有显示让用户授予权限的对话框,则意味着它没有权限,因此您可以使用 ContextCompat.checkSelfPermission() 方法或执行运行时权限检查检查权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多