.NET MAUI Camera Not Opening on Android 15 (Works on Other Versions) - Stack Overflow

In my .NET MAUI application, I have the option to open the camera using MediaPicker.CapturePhotoAsync()

In my .NET MAUI application, I have the option to open the camera using MediaPicker.CapturePhotoAsync(). The camera works fine on other Android versions, but on Android 15, it does not open.

Below is the code I am using to open the camera:

try
{
    photo = await MediaPicker.CapturePhotoAsync();
    if (photo != null)
    {
        var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        using (var stream = await photo.OpenReadAsync())
        using (var newStream = File.OpenWrite(newFile))
            await stream.CopyToAsync(newStream);

        profileImage.Source = newFile;
        isPicture = true;
    }
}
catch (Exception ex)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}

I also tried the below approach, but this also does not work on Android 15:

try
{
    if (MediaPicker.Default.IsCaptureSupported)
    {
        FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo != null)
        {
            picPath = photo.FullPath;

            // Open the photo as a stream
            using Stream sourceStream = await photo.OpenReadAsync();

            // Load the image using SkiaSharp
            using SKBitmap originalBitmap = SKBitmap.Decode(sourceStream);

            // Define the desired width and height
            int desiredWidth = 800; // change as needed
            int desiredHeight = 600; // change as needed

            // Resize the image
            using SKBitmap resizedBitmap = originalBitmap.Resize(new SKImageInfo(desiredWidth, desiredHeight), SKFilterQuality.High);

            // Encode the resized image to a byte array
            using SKImage image = SKImage.FromBitmap(resizedBitmap);
            using SKData encodedData = image.Encode(SKEncodedImageFormat.Jpeg, 75); // 75 is the quality, you can change it
            byte[] resizedImageBytes = encodedData.ToArray();

            // Save the resized image to local storage
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            File.WriteAllBytes(localFilePath, resizedImageBytes);

            // Set the image source
            profileImage.Source = localFilePath;
            isPicture = true;
            isCamera = true;

            // Update other variables
            imageByteContents = new ByteArrayContent(resizedImageBytes);
        }
        else
        {
            isPicture = true;
            isCamera = true;
        }
        isGallery = false;
    }
}
catch (Exception exception)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {exception}");
}

My problem is the camera does not open on Android 15, while it works fine on other Android versions. Please suggest a solution.

Update

I have resolved this issue by adding the IMAGE_CAPTURE in manifest. Now I am able to open the camera in android 15.

<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

But my current problem is after taking photo the photo is reversing.

The Image I clicked:

Image on the UI after tick mark selection:

This issue is only on Android 15 version devices.

In my .NET MAUI application, I have the option to open the camera using MediaPicker.CapturePhotoAsync(). The camera works fine on other Android versions, but on Android 15, it does not open.

Below is the code I am using to open the camera:

try
{
    photo = await MediaPicker.CapturePhotoAsync();
    if (photo != null)
    {
        var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        using (var stream = await photo.OpenReadAsync())
        using (var newStream = File.OpenWrite(newFile))
            await stream.CopyToAsync(newStream);

        profileImage.Source = newFile;
        isPicture = true;
    }
}
catch (Exception ex)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}

I also tried the below approach, but this also does not work on Android 15:

try
{
    if (MediaPicker.Default.IsCaptureSupported)
    {
        FileResult photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo != null)
        {
            picPath = photo.FullPath;

            // Open the photo as a stream
            using Stream sourceStream = await photo.OpenReadAsync();

            // Load the image using SkiaSharp
            using SKBitmap originalBitmap = SKBitmap.Decode(sourceStream);

            // Define the desired width and height
            int desiredWidth = 800; // change as needed
            int desiredHeight = 600; // change as needed

            // Resize the image
            using SKBitmap resizedBitmap = originalBitmap.Resize(new SKImageInfo(desiredWidth, desiredHeight), SKFilterQuality.High);

            // Encode the resized image to a byte array
            using SKImage image = SKImage.FromBitmap(resizedBitmap);
            using SKData encodedData = image.Encode(SKEncodedImageFormat.Jpeg, 75); // 75 is the quality, you can change it
            byte[] resizedImageBytes = encodedData.ToArray();

            // Save the resized image to local storage
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            File.WriteAllBytes(localFilePath, resizedImageBytes);

            // Set the image source
            profileImage.Source = localFilePath;
            isPicture = true;
            isCamera = true;

            // Update other variables
            imageByteContents = new ByteArrayContent(resizedImageBytes);
        }
        else
        {
            isPicture = true;
            isCamera = true;
        }
        isGallery = false;
    }
}
catch (Exception exception)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {exception}");
}

My problem is the camera does not open on Android 15, while it works fine on other Android versions. Please suggest a solution.

Update

I have resolved this issue by adding the IMAGE_CAPTURE in manifest. Now I am able to open the camera in android 15.

<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

But my current problem is after taking photo the photo is reversing.

The Image I clicked:

Image on the UI after tick mark selection:

This issue is only on Android 15 version devices.

Share Improve this question edited Mar 28 at 8:52 Matthew Pans asked Mar 21 at 15:51 Matthew PansMatthew Pans 8751 gold badge11 silver badges29 bronze badges 2
  • For raising issues with CommunityToolkit.Maui and the Camera component you may find you have better success collaborating with the community and developers over at github/CommunityToolkit/Maui/discussions – Stephen Quan Commented Mar 22 at 23:32
  • @StephenQuan I have updated the question with new findings. Could you please check – Matthew Pans Commented Mar 28 at 8:53
Add a comment  | 

1 Answer 1

Reset to default 0

After testing, I used 9.0 Maui to display the image on Android 15 with the following code, and there was no rotation problem.

You can refer to the following code snippet.

try
{
    var photo = await MediaPicker.CapturePhotoAsync();
    if (photo != null)
    {
        var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        using (var stream = await photo.OpenReadAsync())
        using (var newStream = File.OpenWrite(newFile))
            await stream.CopyToAsync(newStream);
 
        profileImage.Source = newFile;
    }
}
catch (Exception ex)
{
    Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744341461a4569427.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信