This piece of code:
mUploadMessage.OnReceiveValue(new Uri[] { result });
results in: Error CS0029 Cannot implicitly convert type 'Android.Net.Uri' to 'System.Uri'
in Visual Studio 2022, using Android.Net and Xamarin.
I have attempted to do type conversion, such as:
mUploadMessage.OnReceiveValue(new Uri[] { (Uri)result });
then I get the error: Error CS0030 Cannot convert type 'Android.Net.Uri' to 'System.Uri'
I also attempted:
mUploadMessage.OnReceiveValue(new Uri[] { result as Uri });
but, then the error is: Error CS0039 Cannot convert type 'Android.Net.Uri' to 'System.Uri' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
So, I attempted a longer conversion:
Android.Net.Uri result = intent.Data;
String urlResult = result.ToString();
Uri sysUrl = new System.Uri(urlResult);
mUploadMessage.OnReceiveValue(new Uri[] { sysUrl });
but, then the error generated is: Error CS1503 Argument 1: cannot convert from 'System.Uri[]' to 'Java.Lang.Object?'
Next, I attempted this:
Android.Net.Uri result = intent.Data;
String urlResult = result.ToString();
mUploadMessage.OnReceiveValue(urlResult);
which compiles without an error, but then on executing the functionality I get: Java.Lang.ClassCastException Message=java.lang.String cannot be cast to android.net.Uri[]
Finally, I attempted this:
Android.Net.Uri result = intent.Data;
mUploadMessage.OnReceiveValue(result);
which resulted in: Java.Lang.ClassCastException Message=android.net.Uri$HierarchicalUri cannot be cast to android.net.Uri[]
The original routine is:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Android.Net.Uri result = intent.Data;
mUploadMessage.OnReceiveValue(new Uri[] { result });
mUploadMessage = null;
}
}
Here is the code from MainActivity.cs:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Android.Webkit;
using Xamarin.Forms.Platform.Android;
using Android.Content;
using System.ComponentModel.Design;
namespace myapp.Droid
{
[Activity(Label = "myapp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static IValueCallback mUploadMessage;
public static int FILECHOOSER_RESULTCODE = 1;
public static IValueCallback mUMA;
public static int FCR = 1;
public static IValueCallback mUploadCallbackAboveL;
public static int PHOTO_REQUEST = 10023;
public static Uri imageUri;
public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
WebView webview = new WebView(this);
SetContentView(webview);
webview.Settings.UseWideViewPort = true;
webview.Settings.LoadWithOverviewMode = true;
webview.Settings.DefaultTextEncodingName = "UTF-8";
webview.Settings.JavaScriptEnabled = true;
webview.Settings.DomStorageEnabled = true;
webview.Settings.AllowFileAccessFromFileURLs = true;
webview.Settings.JavaScriptCanOpenWindowsAutomatically = true;
webview.Settings.AllowUniversalAccessFromFileURLs = true;
webview.Settings.AllowContentAccess = true;
webview.Settings.AllowFileAccess = true;
webview.SetWebViewClient(new OverWebView(this));
webview.SetWebChromeClient(new myWebChromeClient(this));
webview.LoadUrl("https://mywebapp.net");
}
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);
}
public class OverWebView : WebViewClient
{
// For API level 24 and later
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.Settings.JavaScriptEnabled = true;
view.Settings.DomStorageEnabled = true;
view.Settings.AllowFileAccessFromFileURLs = true;
view.Settings.JavaScriptCanOpenWindowsAutomatically = true;
view.LoadUrl(request.Url.ToString());
return false;
}
private MainActivity mainActivity;
public OverWebView(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
}
public class myWebChromeClient : WebChromeClient
{
Activity mActivity = null;
public myWebChromeClient(Activity activity)
{
mActivity = activity;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
MainActivity.mUploadMessage = filePathCallback;
PhotoUtils.openFileChooseProcess(mActivity);
return true;
}
}
public class PhotoUtils
{
private static string TAG = "PhotoUtils";
public static void openFileChooseProcess(Activity activity)
{
Intent i = new Intent(Intent.ActionGetContent);
i.AddCategory(Intent.CategoryOpenable);
i.SetType("image/*");
activity.StartActivityForResult(Intent.CreateChooser(i, "UploadImage"), MainActivity.FILECHOOSER_RESULTCODE);
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Android.Net.Uri result = intent.Data;
mUploadMessage.OnReceiveValue(result);
mUploadMessage = null;
}
}
}
}
CodePudding user response:
As the error messages tell you, your attempts to refer to class Uri
are being compiled as System.Uri
. That's not what you need.
You need Android.Net.Uri
.
Simplest fix is to specify WHICH NAMESPACE you want Uri
in:
mUploadMessage.OnReceiveValue(new Android.Net.Uri[] { result });
An alternative fix is to make a using
that declares this:
using Uri = Android.Net.Uri;