Media picker

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IMediaPicker interface. This interfaces lets a user pick or take a photo or video on the device. The IMediaPicker interface is exposed through the MediaPicker.Default property.

The MediaPicker and IMediaPicker types are available in the Microsoft.Maui.Media namespace.

Get started

To access the media picker functionality, the following platform-specific setup is required.

The CAMERA, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE permissions are required, and must be configured in the Android project. These 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:

    // Needed for Picking photo/video
    [assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)]
    
    // Needed for Taking photo/video
    [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
    [assembly: UsesPermission(Android.Manifest.Permission.Camera)]
    
    // Add these properties if you would like to filter out devices that do not have cameras, or set to false to make them optional
    [assembly: UsesFeature("android.hardware.camera", Required = true)]
    [assembly: UsesFeature("android.hardware.camera.autofocus", Required = true)]
    

    - 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" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    

If your project's Target Android version is set to Android 11 (R API 30) or higher, you must update your Android Manifest with queries that use Android's package visibility requirements.

In the Platforms/Android/AndroidManifest.xml file, add the following queries/intent nodes the manifest node:

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>

Using media picker

The IMediaPicker interface has the following methods that all return a FileResult, which can be used to get the file's location or read it.

  • PickPhotoAsync
    Opens the media browser to select a photo.

  • CapturePhotoAsync
    Opens the camera to take a photo.

  • PickVideoAsync
    Opens the media browser to select a video.

  • CaptureVideoAsync
    Opens the camera to take a video.

Each method optionally takes in a MediaPickerOptions parameter type that allows the Title to be set on some operating systems, which is displayed to the user.

Important

All methods must be called on the UI thread because permission checks and requests are automatically handled by .NET MAUI.

Take a photo

Call the CapturePhotoAsync method to open the camera and let the user take a photo. If the user takes a photo, the return value of the method will be a non-null value. The following code sample uses the media picker to take a photo and save it to the cache directory:

public async void TakePhoto()
{
    if (MediaPicker.Default.IsCaptureSupported)
    {
        FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo != null)
        {
            // save the file into local storage
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);

            using Stream sourceStream = await photo.OpenReadAsync();
            using FileStream localFileStream = File.OpenWrite(localFilePath);

            await sourceStream.CopyToAsync(localFileStream);
        }
    }
}

Tip

The FullPath property doesn't always return the physical path to the file. To get the file, use the OpenReadAsync method.