1

I have moved all my images from the folder mipmap to drawable because here it was said that the mipmap folder is only for app icons to lanch the app Mipmaps vs. drawable folders (the answer got 841 likes). However, when I now want to start my app I get an error message

 FATAL EXCEPTION: main
    Process: com.example.td.barapp, PID: 4331
    java.lang.RuntimeException: Canvas: trying to draw too large(188394348bytes) bitmap.
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)

In several realted questions people tell to store the pictures in different drawable folders (see Android : Understanding drawable folder) like drawable-xxhdpi or drawable-xxxhdpi.

Update: I was told by someone in this post (who later deleted his answers) to store the image (which has a size of 1,7 MB) in the folder drawable-anydpi. I did as he said but the outcome on the Emulator looks quite bad for an imageview (see screenshot): Comparison of the emulator output of the same file Then I moved the same file back into the folder mipmap-xxxhdpi and now it looks good again. So my question now is, whether it is also okay to have the image in the mipmap-xxxhdpi folder instead of the drawable folder? I'd be happy for every advice because I am quite confused now.

VanessaF
  • 515
  • 11
  • 36
  • 2
    user external libraries, either Picasso or Glide to load your images. – Haider Saleem Feb 16 '20 at 17:37
  • Thanks Haider for your answer. As I am new to Android programming I have to admit that I do not understand why to use an external library for loading images. So far loading images has not been a problem for me if I put them into the mipmap directory. I can just directly refer to them in the XML code. What use is it to use Picasso or Glide? – VanessaF Feb 16 '20 at 21:05
  • 1
    Well, you can put them in drawable-anydpi, but handling them with external libraries can serve you better as they offer other options too. – Haider Saleem Feb 16 '20 at 21:55
  • Thanks Haider for your answer. What other options do I have when using such external libararies? – VanessaF Feb 17 '20 at 19:41
  • 1
    you get options like, image center crop, center round and others. here's a link to glide https://github.com/bumptech/glide – Haider Saleem Feb 18 '20 at 07:24
  • Thanks Haider for your answer. I had a look at the librarys and I am not sure whether I really need them as I do not want to load images from the web and I want to store the images in separate drawable folders (see answer below). At the same time I want to design my layout with Android Studio – VanessaF Feb 22 '20 at 14:07
  • @HaiderSaleem: you wrote that "you get options like, image center crop, center round and others". You also can simply use these options in the Layout Editor of Android Studio if you adjust the ScalType of an ImageView. So I still do not understand why I need those libraries – VanessaF Feb 23 '20 at 11:47

1 Answers1

1

java.lang.RuntimeException: Canvas: trying to draw too large(188394348bytes) bitmap

This exception mainly because you are trying to display a big image ~ 188MB which will have a high resolution, and android puts limitation on that, This also reported here.

One of the reasons is to save device resources; for instance you might get java.lang.OutOfMemory, so the system save device memory from that.

1, 2, 3 may guide you thoroughly on that.

Normally you can use a few mega bytes image in your app ~0-3MBs. Third party libraries like Glide & Picasso offer disk, network, & memory management techniques while displaying images on your ImageViews.

One of the options you may consider is to resize images before loading them, so can have look on this question.

Keep in mind that android puts limitaion on your apk file on the app store, so you need to minimize images for that reason, or load them from a back-end server.

As you mentioned, you can only put app icons in res\mipmap. And if you decide to put the images on your app, it's efficient to create them in different densities (hdpi, xhdpi, xxhdpi, xxxhdpi...), so they can match all the variety of device resolutions, and lets you avoid distorted images or high processing by android OS to match your image to the device resolution before displaying them.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks Zain for your answer. The image is not 188 MB, it has 1,7 MB. This is why I was wondering about the error message. But as written and shown in the screenshot before, When I put all the pictures in the mipmap directory everything is okay. When I put them in the drawable folder, I get the error message and when I put them in the drawable-anydpi folder, the resolution looks quite bad (as seen in the picture) – VanessaF Feb 20 '20 at 21:36
  • My question is: Where shall I generally store images in Android? Shall I create different folders for drawable (hdpi, xhdpi, xxhdpi, xxxhdpi...) – VanessaF Feb 20 '20 at 21:37
  • 1
    @VanessaF sorry I miscalculate it; yes you should create folders for all resolutions you want to handle, it's not a must, but best practice; you create them under `MyAppName\app\src\main\res\``.. each folder represents a certain resolution and named with "drawable-" followed by hdpi, xhdpi... Each folder must have the same images with the same names, but with different resolution and sizes – Zain Feb 20 '20 at 22:07
  • 1
    Please check [this](https://material.io/resources/devices/) for more info about densities, sizes, pixels for each resolution – Zain Feb 20 '20 at 22:10
  • 1
    There is also a trade-off, adding different densities, will make your apk size bigger; while saving device battery and decrease processing when the system just pick the appropriate image density without any processing to fit just one image on different device densities. – Zain Feb 20 '20 at 22:19
  • Thanks for the comments Zain. I have two related question. In the android developers guide (https://developer.android.com/training/multiscreen/screendensities#java) it is stated that e.g. for xxxhdpi the resolution should be 192x192 pixels. First of all I think that his is way too small. I tried it and the image in the app looks bad. Have I misunderstood something? Furthermore, if my originial image (taken by a camera) has another format (in my case 3249 x 2005) then scalling to equal length and high (192 x 192)the picture will be distorted. How can I tackle that? – VanessaF Feb 22 '20 at 11:54
  • 1
    @VanessaF Fore sure 192x192 can't fit for all image sizes; this is just an example to show you the ratio between different densities. If you notice the quote from url you provided > you should follow the 3:4:6:8:12:16 scaling ratio between the six primary densities. This is just a ratio, it assumed that you already have an image of 48x48 pixels for mdpi; then you can use the ratio to calculate the xxhdpi by multiplying by 4 (48*4=192) – Zain Feb 22 '20 at 12:15
  • Thanks Zain for the answer. But how shall I know how many pixels I should use for the different resolution types (hdpi, xhdpi, xxhdpi etc)? Suppose I have a photo with a resolution of (3200 x 2000). How can I infer the pixel sizes for the differnt resolutions? – VanessaF Feb 22 '20 at 12:19
  • 1
    Please check [this](https://imgur.com/VzT7ih7) image for ratio illustration ... let me examine the later question – Zain Feb 22 '20 at 12:24
  • 1
    @VanessaF mainly resolution calculation is not a developer task; it is a designer one, [here](https://graphicdesign.stackexchange.com/questions/30431/how-to-make-drawable-of-android-app-for-all-density#answer-30455) you can find some clue; there are plug and play tools out there, I can't recommend one as this is prohibited in SO community; but you can google that, and my recommendation is that if you're not a designer, then don't be bothered for this; just pick a plug-and play tool that fits your needs. – Zain Feb 22 '20 at 12:53
  • Thanks for your comments Zain. What exactly do you mean by plug and play tool? I undestand that you can't tell me one specific one. But what should that tool be able to do? Are you talking about something like Glide or Picasso for Android? – VanessaF Feb 22 '20 at 14:05
  • 1
    @VanessaF, welcome.. I mean that you can just provide your (3200 x 2000) image (of course it must be a high resolution image), and then the tool automatically generates different resolution images that you need within their respective drawable folders – Zain Feb 22 '20 at 14:08
  • Thanks Zain for your answer. Maybe one last question about picasso and glide library. People told be to use it. I had a look at the librarys and I am not sure whether I really need them as I do not want to load images from the web and I want to store the images in separate drawable folders (and not scale one image of the folder drwable-anydpi). At the same time I want to design my layout with Android Studio. So far I do not see how I can profit from such libraries. – VanessaF Feb 23 '20 at 11:24
  • 1
    @VanessaF if you're going to use them without recycling them in ListView or RecyclerView, then I guess no need to a library (not totally sure about that); but if you will use them in a RecyclerView list where photos will be recycled off/on the screen; then glide will improve the performance of recycling those images. you'll notice some delay while scrolling a big list of images without using glide. wish that answer your question – Zain Feb 23 '20 at 20:28