103

I have a published app that is crashing at startup on Android N when the newly introduced Display size OS setting is set to too large a value.

When I look in logcat, I see the following message:

java.lang.RuntimeException: Canvas: trying to draw too large(106,975,232 bytes) bitmap.

I've traced the issue to an ImageView in my first Activity that shows a nice big background image. The image in question is 2048x1066 and is in my generic drawables directory, so no matter the density, this image will be used.

Everything works okay when the Display size setting is Small. But when I go up to Default, it stops working. If I then swap the image out with a smaller one, it works at Default, but if I go up to Large, it stops working again.

My guess is that adjusting Display size up causes your device to behave like a physically smaller device with a higher pixel density. But I don't understand what I'm supposed to do here. If I put in progressively smaller images for progressively higher resolutions, it won't look good on actually large displays. Or am I not understanding something?

Any pointers would be greatly appreciated.

8
  • 28
    "The image in question is 2048x1066 and is in my generic drawables directory, so no matter the density, this image will be used" -- res/drawable/ is a synonym for res/drawable-mdpi/. If you want the image to not be scaled based upon density, use res/drawable-nodpi/ or res/drawable-anydpi/. Aug 23, 2016 at 23:21
  • 3
    "Are you saying that a 100x100 pixel image living in various different resource directories is actually scaled to create a virtual different-resolution version of it before going on to the layout?" -- that depends upon what densities you have and what device you are running on. If there is an exact match, nothing is resampled. If there is not an exact match, a nearby density's image is resampled. So, if you only have res/drawable/foo.png (a.k.a., res/drawable-mdpi/foo.png), and your device is xhdpi, the image will be doubled along both axes, taking up 4x the memory. Aug 24, 2016 at 0:02
  • 6
    The 106975232 value in your error happens to be exactly 49x the image resolution, implying 7x resampling along both axes. That's a lot higher than I would have expected. I didn't get a chance yet to play around with the display size settings in Android 7.0, so I'll add that to my ever-growing to-do list... Aug 24, 2016 at 0:06
  • 7
    Nice catch on the 49x! I think I can explain why it's so high. Keep in mind that that number is bytes. This image is 24-bit, but it's probably read in at 32 bits per pixel. That would make it 8732672 bytes in memory, which goes into that figure exactly 12.25 times, which in turn implies 3.5x scaling along each axis. This device is xxhdpi, so that seems like it might be about right. Either way, I had no idea the resources would be resampled like that. Thank you for the help! (By the way, moving the image to drawable-nodpi does indeed fix it.)
    – Brian Rak
    Aug 24, 2016 at 0:29
  • 1
    You really should accept johan's answer May 31, 2017 at 3:57

14 Answers 14

191

I my case, moving the (hi-res) splash bitmap from drawable to drawable-xxhdpi was the solution.

I had the same problem. I didn't suspect my splash screen to be the problem, since it is displayed when the app is started, but it turned out the splash screen is the problem.

The splash screen in my case has xxhdpi resolution, and it was mistakenly placed in the drawable folder, instead of drawable-xxhdpi. This made Android assume the splash screen had mdpi resolution and scale the image to 3*3 times it's required size and trying to create a bitmap.

2
  • 1
    i'm guessing: so, android scale a image from xxhdpi to hdpi for example to be a lighter image? as you said from 3*3 -> 1/1 to 1/3 for example? Jul 10, 2017 at 22:36
  • As kalsara Magamage notes, this is now mipmap-xxhdpi. Mar 21, 2018 at 21:49
57

I solved the problem after adding the below code into the Manifest file's application tag in between android: lines.

android:hardwareAccelerated="false"
8
  • 4
    Well, after spending some hours this solution works for xiaomi and Samsung mobile problems. Aug 18, 2019 at 9:09
  • 13
    This will result in disabling all the elevations of CardView. So the proper solution would be to scale the bitmap to smaller size. Feb 19, 2020 at 12:15
  • 3
    Instead of disabling hardware acceleration in Application level controlling it in Activity, Window, View level will be more useful. Apr 13, 2020 at 16:09
  • 1
    But, there is some other problem raise after doing android:hardwareAccelerated="false" . showing an odd view in some device that has an API level below 22. Jun 20, 2020 at 18:17
  • 10
    Don't do that, it slows down your app.
    – Curio
    Aug 23, 2020 at 9:32
15

I don't know would it help some one, but I'll just leave it here. In my case - problem was only on Sumsung devices with Android 7, and problem was in splash screen proportions. after changing height to 1024 px - everything works fine

4
  • My issue was bitmap (jpg, png) in drawable (without suffix) folder. It's my stupidity, but maybe it helps someone :-)
    – gingo
    Jun 3, 2017 at 8:43
  • This is helpful! Same problem only Samsung... What did you have first and How did you fix it ?
    – M'hamed
    Apr 9, 2018 at 15:12
  • 1
    Were you trying to load the images from URL. I am facing the same issue with only Samsung Galaxy S6 on Android 7. I am loading images in RecyclerView. I still can not figure this out. @M'hamed Aug 15, 2020 at 7:29
  • 3
    I had this issue with some SAMSUNG S6, S6 Active with Android 7. So I set up further analysis with Firebase and found out all these devices all have DPI settings above 500. Then I found a way to reproduce this in the emulator(I used MEMU here), I set my emulator with a screen resolution exactly the same with Galaxy S6 "2560x1440, DPI 577" and my app produces the error as expected. It's caused by the high DPI, many SAMSUNG devices have a high DPI setting. I placed hi-res bitmap images in "drawable", it only works for devices that have a DPI less than 500. Moving them to "-xxhdpi-v4" is the fix.
    – Lynch Chen
    May 2, 2021 at 20:07
9

Move your image in the drawable to mipmap-xxhdpi.Your image is in bitmap format so you should put your image in mipmap folder,then it will work

2
  • 3
    AFAIK the mipmap folder is for launcher icons only. I've never used it for anything else. See stackoverflow.com/a/28065664/4034572 Aug 26, 2019 at 8:59
  • Yeah this worked for me. I had images sizes about 1MB and I put them in mipmap-xxhdpi
    – user12465043
    Feb 9, 2020 at 14:09
6

There are some scenarios where Original Bitmap needs be Drawn into ImageViews, Photo Editing apps etc...,

as bay mentioned above setting

android:hardwareAccelerated="false"

will Cause bad UI experince, You can set hardwareAccelerated Only one selected Activity where high res image to be drawn

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>
3

Try to use Bitmap.Factory class, this link will help you Loading Large Bitmaps Efficiently

1

if you use Picasso change to Glide like this.

Remove picasso

Picasso.get().load(Uri.parse("url")).into(imageView)

Change Glide

Glide.with(context).load("url").into(imageView)

More efficient

2
  • 3
    I am still getting the error even after switching to Glide from Picasso. On Samsung J6 phone. And Moto Z2 :( Aug 20, 2020 at 0:28
  • 1
    Glide does have some benefits over Picasso e.g. speed but Picasso is not broken so you cannot suggest another library to fix 1 issue with it. We are not even sure this is a Picasso's problem or it just shows up in Picasso stacktrace
    – ericn
    Feb 21, 2021 at 16:26
0

The icon files are too large for Android to efficiently and smoothly load. Android recognizes this with its smart algorithms.

You can resize the icon files using Final Android Resizer by asystat. Resize them to "xhdpi" or lower.

Place the resized photos in drawable or overwrite over the existing large icon files.

Then, you're done.

0

if you are using glide and you are loading 1k of images at a time or some images then it is issue of glide or whatever you are doing to use to set the image view. you can resolve it just by applying scale type in glide.

0

In my case, I just changed the canvas of image which is used in the background using Paint3d(or you can use any other). Here I am sharing a screenshot just go through it.

1
  • Please use comments to share your thoughts.
    – Ajay Sivan
    Apr 12, 2019 at 12:26
0

it is solved by resizing the images to a lower size.

0

Need to add Manifest file's application tag in between android: add below lines.

android:hardwareAccelerated="false"
0

Just an addition to the Johan Franzén's answer, maybe it's a good idea to not only add drawable-xxhdpi density folder, but also add another density folder.
So whatever the android version and size, your app can prepare the image source with the right size :

  1. Change your folder view on the top left from Android to Project
  2. Go to YourProjectFolder folder > app > src > main > res
  3. Prepare the original image with your best resolution, and split it into each folder size automatically. You can do it in Baker
  4. Then create a folder with another density, namely:
  5. drawable-hdpi, drawable-ldpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
  6. Put each image into the appropriate folder
0

i solved the problem by simply changing the image extension to .png extension, and it worked just fine with me.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 22, 2022 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.