How to Open Files on Xamarin Android Using FilePicker

Ronald GANS 136 Reputation points
2023-03-21T13:53:33.71+00:00
using Xamarin.Essentials; (at the top of the file)
async Task<FileResult> PickAndShow(PickOptions options)
{

    
    try
    {
        var result = await FilePicker.PickAsync(options);
        if (result != null)
        {
            string Text = $"File Name: {result.FileName}";
            if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
            {
                var stream = await result.OpenReadAsync();
                Image = ImageSource.FromStream(() => stream);
            }
        }

        return result;
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }

    return null;
}

But I get this error:

The name FilePicker does not exist in the current context.

There are several other similar errors.

Any help is very appreciated.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,298 questions
{count} votes