Forum Xamarin Cross Platform with Xamarin

How to convert URI to File Path from a content page

Hi

I am fairly new to Xamarin. I have used the Plugin.FilePicker.CrossFilePicker to load a file but it is giving me a URI instead of the file path on some devices. How do I convert the URI to the actual file path?

There seems to be a lot of documentation on converting URI to a path but it all seems to be android specific with no examples in a cross platform environment.

Sample Code:

// CONTENT PAGE

public partial class LoadInspection : ContentPage
{
private async Task PickAndShowFile(string[] fileTypes)
{
try
{
var pickedFile = await Plugin.FilePicker.CrossFilePicker.Current.PickFile(fileTypes);

            if (pickedFile != null)
            {
                fn = pickedFile.FilePath;

                if(fn.StartsWith("content://"))
                {
            // convert the URI to a file path
            fn = URIpath????
                }


                if(!fn.EndsWith(".vsi"))
                {
                    await DisplayAlert("Error", "The selected file '" + fn  + "' is not a valid .vsi file", "OK");
                    return;
                }

                inspectionData.loadInspectionFile(fn);

                menuPage = new NavigationPage(new Pages.MainMenu(inspectionData)) { Title = "Main Menu", BarBackgroundColor = Color.FromHex(Models.ColorConfig.barColor) };
                await App.Current.MainPage.Navigation.PushAsync(menuPage);
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.ToString();
            fn = string.Empty;
        }
    }

}

// MainActivity.cs
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();

        if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
        {
            Android.Support.V4.App.ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.WriteExternalStorage }, 0);
        }

        if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
        {
            Android.Support.V4.App.ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 0);
        }

        LoadApplication(new App());
        Window.SetStatusBarColor(Android.Graphics.Color.Argb(255, 59, 76, 92));
    }
}

Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    Try to convert url to file path on each platform, then pass the path result to PCL using DependencyService. Just call the method and pass the uri in ContentPage, you'll get the path.

    • Create an interface
    • Implement the interface on each platform
    • Register the platform implementations
    • Resolve the platform implementations

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    Try to convert url to file path on each platform, then pass the path result to PCL using DependencyService. Just call the method and pass the uri in ContentPage, you'll get the path.

    • Create an interface
    • Implement the interface on each platform
    • Register the platform implementations
    • Resolve the platform implementations
  • retroheadretrohead Member

    Thanks Jarvan, that's exactly what I needed and I am sure will come in handy.

    I found this simple video showing an implementation of the Dependency Service:

    xxxx://www.youtube.com/watch?v=Tmsmlt4aVLk

Sign In or Register to comment.