File picker
This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IFilePicker
interface. With the IFilePicker
interface, you can prompt the user to pick one or more files from the device. The IFilePicker
interface is exposed through the FilePicker.Default
property.
The FilePicker
and IFilePicker
types are available in the Microsoft.Maui.Storage
namespace.
Get started
To access the FilePicker functionality, the following platform specific setup is required.
The ReadExternalStorage
permission is required and must be configured in the Android project. This can be added in the following ways:
Add the assembly-based permission:
Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after
using
directives:[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)]
- or -
Update the Android Manifest:
Open the Platforms/Android/AndroidManifest.xml file and add the following in the
manifest
node:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Important
All methods must be called on the UI thread because permission checks and requests are automatically handled by .NET MAUI.
Pick a file
The PickAsync
method prompts the user to pick a file from the device. Use the PickOptions
type to specify the title and file types allowed with the picker. The following example demonstrates opening the picker and processing the selected image:
public async Task<FileResult> PickAndShow(PickOptions options)
{
try
{
var result = await FilePicker.Default.PickAsync(options);
if (result != null)
{
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
using var stream = await result.OpenReadAsync();
var image = ImageSource.FromStream(() => stream);
}
}
return result;
}
catch (Exception ex)
{
// The user canceled or something went wrong
}
return null;
}
Default file types are provided with FilePickerFileType.Images
, FilePickerFileType.Png
, and FilePickerFilerType.Videos
. You can specify custom file types per platform, by creating an instance of the FilePickerFileType
class. The constructor of this class takes a dictionary that is keyed by the DevicePlatform
type to identify the platform. The value of the dictionary key is a collection of strings representing the file types. For example here's how you would specify specific comic file types:
var customFileType = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "public.my.comic.extension" } }, // or general UTType values
{ DevicePlatform.Android, new[] { "application/comics" } },
{ DevicePlatform.WinUI, new[] { ".cbr", ".cbz" } },
{ DevicePlatform.Tizen, new[] { "*/*" } },
{ DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // or general UTType values
});
PickOptions options = new()
{
PickerTitle = "Please select a comic file",
FileTypes = customFileType,
};
Pick multiple files
If you want the user to pick multiple files, call the FilePicker.PickMultipleAsync
method. This method also takes a PickOptions
parameter to specify additional information. The results are the same as PickAsync
, but instead of the FileResult
type returned, an IEnumerable<FileResult>
type is returned with all of the selected files.
Tip
The FullPath
property doesn't always return the physical path to the file. To get the file, use the OpenReadAsync
method.
Feedback
Submit and view feedback for