Good day to all.
I have researched a lot about how to open the camera through a file type input through a WebView in the Android version (it is a Xamarin.Forms project)
I have tried two ways, the first one, using the PermissionsPlugin library and that of a CustomWebView with their respective Renderer and I have not been able to make it work
Este es mi CustomWebViewRenderer:
public class CustomWebViewRenderer : WebViewRenderer
{
//Activity mContext;
public CustomWebViewRenderer(Android.Content.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.Settings.DomStorageEnabled = true;
Control.Settings.SetPluginState(WebSettings.PluginState.On);
Control.ClearCache(true);
CustomWebClient cwc = new CustomWebClient(MainActivity.Instance);
Control.SetWebChromeClient(cwc);
}
public class CustomWebClient : WebChromeClient
{
Activity mActivity = null;
public CustomWebClient(Activity activity)
{
mActivity = activity;
}
[TargetApi(Value = 21)]
public override void OnPermissionRequest(PermissionRequest request)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
mActivity.RunOnUiThread(() =>
{
request.Grant(request.GetResources());
});
}
}
}
}
This is my MainActivity.cs:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static MainActivity Instance { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Instance = this;
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
CachedImageRenderer.Init(false);
AnimationViewRenderer.Init();
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);
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
and these are the permissions assigned in the AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
Any information on how to achieve this would be greatly appreciated. I wait for a hand.
Best regards