MediaPicker.PickPhotosAsync returns null in some cases
Description
According to docs, PickPhotosAsync should never return null (empty collection if user has cancelled an operation). Sometimes it does though. I don't knwo how to reproduce this relyably, because it sometimes works, sometimes doesn't - for the same photo.
Steps to Reproduce
var photoPickerOptions = new MediaPickerOptions
{
Title = R.SelectProfilePhoto,
MaximumWidth = 200,
SelectionLimit = 1,
RotateImage = true,
PreserveMetaData = true
};
var selectedPhotos = await MediaPicker.Default.PickPhotosAsync(photoPickerOptions); // <<< NULL HERE
var photo = selectedPhotos.FirstOrDefault();
ER:
selectedPhotos result has single item (or zero, if user has cancelled).
AR:
In some cases, selectedPhotos is null.
Link to public reproduction project repository
https://github.com/Dreamescaper/MauiCameraRepro
Version with bug
10.0.10
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
iOS 16.7
Did you find any workaround?
No response
Relevant log output
I'm looking at this code here: https://github.com/dotnet/maui/blob/697ba1a86c0f2e8e341fad674f1aab234b9ebe97/src/Essentials/src/MediaPicker/MediaPicker.ios.cs#L158
Why does it set null here? Is that to handle the case when user dismissed the dialog? In this case: a) It seems to be wrong, as per documentation empty collection should be returned, not null. b) I can see that Handler is also invoked on disposal, not only on dismissal. Which, per my understanding, allows a race condition - whoever set the task result first.
I have added a repro project. I wasn't able to reproduce this issue with iPhone 17 emulator, but it is reproducing pretty consistently on iPhone X. Not sure if something has changed with iOS behavior, or simply iPhone X is a slower device, and it is more likely to hit a race conditions.
Here's the screen recording (taps are not visisble, but I'm always selecting an image here):
https://github.com/user-attachments/assets/d08a1b4c-4ef6-4925-907d-350ddacd2e29
This issue has been validated in Visual Studio Code 1.106.0 with MAUI versions (10.0.10, 10.0-rc2 and 10.0.0-preview7.). It was not reproducible on iOS platform. I have attached the video for reference:
https://github.com/user-attachments/assets/2d6d15c4-1fe5-48a7-98c8-c163363e010b
Please keep in mind that MR https://github.com/dotnet/maui/pull/32656 fixed incorrect contract ([] vs null). but not the issue itself, when I receive null even when image is selected by the user.
I’m encountering the same issue after upgrading my project to .NET 10. The compiled app runs correctly on an iPhone with iOS 26.1, and the media picker works as expected. However, when I tested it on my older iPad Mini 2 running iOS 12.5.7, the media picker returns null.
I wanted to point this out in case it helps narrow down the problem. Should I consider raising the minimum iOS version in my .csproj file from 12.4 to 15?
I am also getting a null result when picking a photo on windows (haven't tried other platforms yet)
it works when I call
await Microsoft.Maui.Media.MediaPicker.PickPhotosAsync(new MediaPickerOptions { SelectionLimit = 1})
but not when I set the MaximumWidth, eg
await Microsoft.Maui.Media.MediaPicker.PickPhotosAsync(new MediaPickerOptions { SelectionLimit = 1, MaximumWidth = 1280 })
this exception is thrown
System.ObjectDisposedException: 'Cannot access a closed Stream.'