dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

[Android] Loading a large size image throws Java.Lang.RuntimeException #23828

Open VishalOmprasad opened 1 month ago

VishalOmprasad commented 1 month ago

Description

When attempting to load a large size image in a .NET MAUI application on the Android platform, the application throws a Java.Lang.RuntimeException. This issue significantly impacts the app's performance and stability, causing it to crash unexpectedly.

image

Steps to Reproduce

  1. Download and open the attached .NET MAUI project.
  2. Select the framework as Android.
  3. Deploy and run the application on an Android device or simulator.
  4. Click the "Show Image" button.
  5. App will crash with "Java.Lang.RuntimeException".

Link to public reproduction project repository

https://github.com/VishalOmprasad/NET-MAUI-Samples/tree/main/ImageCrash

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

RoiChen001 commented 1 month ago

I can repro this issue at Android platform on the latest 17.11.0 Preview 5.0 (8.0.70&8.0.61).

dellis1972 commented 1 month ago

So the problem here is the image is just too big to be handled by the hardware on the device.

The image is 6960 x 4640 pixels. As a png that is 1.5 meg. The problem is that it is NOT 1.5 meg once loaded. It is an RGBA image so 6960 x 4640 x 4 (one byte for each rgba channel). So unpacked the image is 129177600 bytes. 129 meg!

A quote from the google engineer https://issuetracker.google.com/issues/244854452#comment3

The crash itself is working as intended, so closing the bug as such. As for why you're hitting this, the platform guarantees that you can always draw a bitmap up to the same size as the display resolution, but not necessarily larger. 

So if the screen resolution is 1920x1080 we get 192010804 = 8294400 bytes. So this will always crash if you try to render an image that is larger than the screen resolution.

That said, images which do not need scaling should end up in the drawable-nodpi folder, not the drawable folder.

dellis1972 commented 1 month ago

trying to use drawable-nodpi seems to break a bunch of tests and image layouts. So maybe that is not a good idea to do by default.

dellis1972 commented 1 month ago

So I have an idea. We can limit the size of images we load using the glide tricks. The downside is that it will happen automatically, if you try to load an image larger than the screen size it would automatically be scales down to fit within the screen resolution.

The question to @VishalOmprasad is would that be preferable to a runtime crash ? It would mean the size of the image you load would depend on the device the user is running.