Training
Learning path
Build mobile and desktop apps with .NET MAUI - Training
In this learning path, use C# and Visual Studio with .NET MAUI to create an app that runs across iOS, Android, and Windows.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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>
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.
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.
.NET MAUI feedback
.NET MAUI is an open source project. Select a link to provide feedback:
Training
Learning path
Build mobile and desktop apps with .NET MAUI - Training
In this learning path, use C# and Visual Studio with .NET MAUI to create an app that runs across iOS, Android, and Windows.
Documentation
Learn how to use the IScreenshot interface in the Microsoft.Maui.Media namespace, to capture of the current displayed screen of the app.
.NET MAUI - Platform integration - Code Samples
This sample demonstrates how to use cross-platform APIs for common device features.
CameraView - .NET MAUI Community Toolkit - Community Toolkits for .NET
The CameraView provides the ability to connect to a camera, display a preview from the camera and take photos.