Media picker for photos and videos

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IMediaPicker interface. This interface lets a user pick or take a photo or video on the device.

The default implementation of the IMediaPicker interface is available through the MediaPicker.Default property. Both the IMediaPicker interface and MediaPicker class are contained in the Microsoft.Maui.Media namespace.

Get started

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

The CAMERA permission is required and must be configured in the Android project. In addition:

  • If your app targets Android 12 or lower, you must request the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.

  • If your app targets Android 13 or higher and needs access to media files that other apps have created, you must request one or more of the following granular media permissions instead of the READ_EXTERNAL_STORAGE permission:

    • READ_MEDIA_IMAGES
    • READ_MEDIA_VIDEO
    • READ_MEDIA_AUDIO

These permissions can be added in the following ways:

  • Add the assembly-based permissions:

    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, MaxSdkVersion = 32)]
    [assembly: UsesPermission(Android.Manifest.Permission.ReadMediaAudio)]
    [assembly: UsesPermission(Android.Manifest.Permission.ReadMediaImages)]
    [assembly: UsesPermission(Android.Manifest.Permission.ReadMediaVideo)]
    
    // Needed for Taking photo/video
    [assembly: UsesPermission(Android.Manifest.Permission.Camera)]
    [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage, MaxSdkVersion = 32)]
    
    // 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.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />    
    <!-- Required only if your app needs to access images or photos that other apps created -->
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <!-- Required only if your app needs to access videos that other apps created -->
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    <!-- Required only if your app needs to access audio files that other apps created -->
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />    
    

    - or -

  • Update the Android Manifest in the manifest editor:

    In Visual Studio double-click on the Platforms/Android/AndroidManifest.xml file to open the Android manifest editor. Then, under Required permissions check the permissions listed above. This will automatically update the AndroidManifest.xml file.

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 in 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.

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.