📱 Device Features
Camera & Gallery
MediaPicker, CapturePhotoAsync, picking from gallery.

Capturing Photos

csharp
private async void OnCameraClicked(object s, EventArgs e)
{
    if (!await RequestCameraAsync()) return;

    var photo = await MediaPicker.Default.CapturePhotoAsync();
    if (photo is null) return;

    // Copy to app storage for persistence
    var dest = Path.Combine(FileSystem.AppDataDirectory, photo.FileName);
    using var src  = await photo.OpenReadAsync();
    using var dest_stream = File.OpenWrite(dest);
    await src.CopyToAsync(dest_stream);

    CapturedImage.Source = dest;
}

Picking from Gallery

csharp
var photo = await MediaPicker.Default.PickPhotoAsync(
    new MediaPickerOptions { Title = "Pick a profile photo" });

if (photo is not null)
{
    var stream = await photo.OpenReadAsync();
    ProfileImage.Source = ImageSource.FromStream(() => stream);
}
📷
On Android 13+, READ_MEDIA_IMAGES replaces READ_EXTERNAL_STORAGE. MAUI handles this automatically when targeting API 33+.

Key Takeaways

MediaPicker.CapturePhotoAsync opens the camera
MediaPicker.PickPhotoAsync opens the system gallery picker
Copy captured photos to AppDataDirectory for persistent storage
ImageSource.FromStream() displays images without saving them to disk
Lesson 27 of 30Device Features
← Previous Next Lesson →