Maui media picker on Windows Platform error
See original GitHub issueI have created a very basic MauiApp because I wanted to try the MediaPicker on the Windows Platform.
Thus I copied the code from the official documentation and tried to run my application
However if I add <uap:Capability Name="webcam"/>
to the Package.appxmanifest
file as suggested in the documentaion, and run the application it gives me the following error:
Error DEP0700: Registration of the app failed. [0x80080204] error 0xC00CE169: App
manifest validation error: The app manifest must be valid as per schema: Line 39, Column
21, Reason: 'webcam' violates enumeration constraint of 'documentsLibrary
picturesLibrary videosLibrary musicLibrary enterpriseAuthentication
sharedUserCertificates userAccountInformation removableStorage appointments contacts
phoneCall blockedChatMessages objects3D voipCall chat'.
The attribute 'Name' with value 'webcam' failed to parse. MauiApp3
So in order to solve this problem I tried to change the capability from <uap:Capability Name="webcam"/>
to <DeviceCapability Name="webcam"/>
.
In this way I can run the application without errors, but photo
is always null:
public async void TakePhoto(object sender, EventArgs e)
{
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);
}
else
{
// *** IT ALWAYS ENTERS IN THE ELSE CLAUSE ***
// *** BECAUSE photo IS ALWAYS NULL ***
CounterBtn.Text = $"Capture is supported but {photo} is null";
}
}
}
Note: The function above is called when I click to this button that I’ve defined in MainPage.xaml
file:
<Button
x:Name="ImageBtn"
Text="Take Photo"
SemanticProperties.Hint="Take Image"
Clicked="TakePhoto"
HorizontalOptions="Center" />
Stackoverflow question: https://stackoverflow.com/questions/72450077/maui-media-picker-on-windows-platform-error
Issue Analytics
- State:
- Created a year ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
Unfortunately this is a old and known bug for WinUI3. https://github.com/microsoft/WindowsAppSDK/issues/1034
As a workaround you can create a custom mediapicker that inherit everything from the MAUI one for Android, iOS and Catalyst.
Them implement a custom mediapicker for Windows, intheriting all the
Capture...
methods and reimplementing theCapturePhotoAsync
using a custom class for camera capture:This is the class:
The only downside is that with this class you cannot take videos on windows.
Actually, with a slight modification in the custom CameraCaptureUI class, @GiampaoloGabba’s workaround can be used to capture both photos and videos on Windows: