I am using following code to pick a file using FilePicker in xamarin forms. End goal is to give Android Uri to a service in Xamarin.Android project, the service transfers the file to somewhere.
How do I get Android Uri(Android.Net.Uri )from FilePicker result in xamarin forms? Android service only accepts Android.Net.Uri type.
try
{
var result = await FilePicker.PickAsync(new PickOptions {
PickerTitle = "Select zip file"
});
if (result != null)
{
FileNameLabel.Text = $"File Name: {result.FileName}";
if (!result.FileName.EndsWith("zip", StringComparison.OrdinalIgnoreCase))
{
await DisplayAlert("Wrong File", "Selected file is not right", "Ok");
}
}
}
catch (Exception ex)
{
// The user canceled or something went wrong
}
I tried the following:
firmwareFullPath = result.FullPath;
Android.Net.Uri FirmwareUri = Android.Net.Uri.Parse(firmwareFullPath);
But the service gives file not found error. The service works fine in Xamarin.Android project with the same file with native file picker activity where I get the Uri from intent returned after file selection. Below is the comparison on Uri objects, one object generated using :
Android.Net.Uri.Parse(firmwareFullPath);
The other from
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if ((resultCode == Result.Ok) && (data != null))
{
Android.Net.Uri FirmwareUri = data.Data;
}
}
Uri Object from Xamarin.Form FilePicker
Uri Object from Xamarin.Andriod
**Major difference is Authority and EncodedAuthority are null in case of xamarin forms filepicker **