0

I'm displaying an image through an ImageSwitcher with this factory:

    view_background.setFactory {
        val imageView = ImageView(this@MainActivity)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView
    }

And I'm displaying correctly this 2341x1246 PNG 2.55 MB image on a Nexus 5 emulator:

enter image description here

However, if I use it on Nexus 6, it crashes:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.llovagn.t4r, PID: 4693
    java.lang.RuntimeException: Canvas: trying to draw too large(142936136bytes) bitmap.

By putting it in drawable-xxhdpi as suggested in this answer, it now look like this on both devices:

enter image description here

Which I something that I don't really want (the first way is the correct one). Why and how can I solve this?

justHelloWorld
  • 6,478
  • 8
  • 58
  • 138

2 Answers2

0

2341x1246 PNG 2.55 MB

Canvas: trying to draw too large(142936136bytes) bitmap

This doesn't look like a good approach. Reduce the size of the image and put the image in the drawable directory.

It happens because you put the picture inside "drawable-xxhdpi". Take a look at the sizes in here.

However, after doing what I suggested, use FIT_XY to fit the image:

imageView.scaleType = ImageView.ScaleType.FIT_XY

You won't need 2.5 MB image in your application. That's pretty too much with that size.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thank you for your answer! So looking at [this](https://stackoverflow.com/a/13228830/4480180) answer, should I resize all to images to be at most `1280x1920`? – justHelloWorld Oct 08 '18 at 19:06
  • 1
    «...in the drawable directory...» I would suggest drawable-nodpi directory instead (https://commonsware.com/blog/2015/12/21/nodpi-anydpi-wtf.html) – Karma Maker Oct 08 '18 at 19:06
  • 1
    And why the `FIT_XY`? – justHelloWorld Oct 08 '18 at 19:07
  • @justHelloWorld That depends actually. Will you add tablets support to your app in future? It all depends on the devices you are targeting to use the app. Check [this link](https://developer.android.com/guide/topics/manifest/supports-screens-element) for screen supports and remember to add them. – ʍѳђઽ૯ท Oct 08 '18 at 19:09
  • @justHelloWorld From [the documentation](https://developer.android.com/reference/android/widget/ImageView.ScaleType): `Scale the image using Matrix.ScaleToFit.FILL` Depends on you. Maybe you just want to make the image fit center or fill the x,y. However, `FIT_XY` fills the entire page which is a good choice to use. – ʍѳђઽ૯ท Oct 08 '18 at 19:11
  • Nope, it's a phone app only now and forever! – justHelloWorld Oct 08 '18 at 19:12
  • So, `2341x1246` is unnecessary and pretty huge (*Use something like under `1000`* and use online websites to reduce the size). If you add one more image like that with 2.5 MB, users won't like an app with too much size to download. – ʍѳђઽ૯ท Oct 08 '18 at 19:14
0

To avoid this error try to use as small resolution of image as possible, also the image view should also be small, you could also get OOM on bigger and high res devices. Best way would be to use a png image on a background view where background can be solid color or gradient. By this way you can use smaller images that might not need to fill the whole screen size.

NIKHIL MAURYA
  • 289
  • 1
  • 15