147

I'm running Ubuntu 16.04. And on Android Studio when I try to run my application in the emulator I get the following error:

FATAL EXCEPTION: main Process: project name here, PID: 2528 java.lang.RuntimeException: Canvas: trying to draw too large(216090000bytes) bitmap. at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260) at android.graphics.Canvas.drawBitmap(Canvas.java:1415) at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:528) at android.widget.ImageView.onDraw(ImageView.java:1316) at android.view.View.draw(View.java:17185) at android.view.View.updateDisplayListIfDirty(View.java:16167) at android.view.View.draw(View.java:16951) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16162) at android.view.View.draw(View.java:16951) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at
etc...

I did have to run through some hoops to get my emulator working however, needed to create a sym-link so I can run the emulator on AMD. Not sure if this is part of the problem. And for the life of me I cannot figure why it continues to do this. In my group there are others who emulate the project just fine on the same emulated phone and SDK.

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Francisco
  • 2,050
  • 3
  • 14
  • 19
  • have you tried to read the bitmap in a smaller size using `BitmapOption.inSampleSize` ? – L. Swifter Nov 28 '16 at 00:50
  • 1
    sample your bitmap using this https://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Nishant Verma Nov 28 '16 at 00:57
  • 19
    **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. – Zubair Rehman Aug 30 '17 at 06:38
  • Does it happen only on the emulator or on the device with the same screen size as well? – Dmitry Sep 04 '18 at 22:57

17 Answers17

282

Move your image in the (hi-res) drawable to drawable-xxhdpi. But in app development, you do not need to use large image. It will increase your APK file size.

Striezel
  • 3,693
  • 7
  • 23
  • 37
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • 7
    Thanks, that also worked when my image was only 500 kb, but resolution was way too high. – ashishdhiman2007 Jun 08 '17 at 09:59
  • 2
    I am getting this error while loading images from an API. What could be a solution in that case ? – Rohit Singh Aug 14 '20 at 06:34
  • 12
    I ran into the same issue in **Firebase Crashlytics** but it doesn't mention the exact file+line code, So I don't know what is the exact image that causing this crash. Obviously there's a lot of images in my res file, how can I locate the problematic one – ravid rinek Sep 10 '20 at 09:19
  • You saved my day, BTW I was getting the same error in react native once ejected expo, might help some one. – Dinesh Kachhot Mar 19 '21 at 16:07
  • This also happen to me while scrolling through pictures using a RecyclerView. Lucky for me I had a choice of HI or Low Definition URLs to choose from. Using all low Def URLs corrected this problem for the RecyclerView as well. Problem happens in both emulator and device. – BroPage Aug 07 '21 at 17:22
  • Well, the solution does not help at all, the error is the same – Oleg991 Aug 13 '23 at 17:08
48

The solution is to move the image from drawable/ folder to drawable-xxhdpi/ folder, as also others have mentioned.

But it is important to also understand why this somewhat weird suggestion actually helps:

The reason is that the drawable/ folder exists from early versions of android and is equivalent to drawable-mdpi. When an image that is only in drawable/ folder is used on xxhdpi device, the potentially already big image is upscaled by a factor of 3, which can then in some cases cause the image's memory footprint to explode.

Nace
  • 2,854
  • 1
  • 15
  • 21
34

This solution worked for me.

Add these lines in your Manifest application tag

android:largeHeap="true"
android:hardwareAccelerated="false"

    
Abdul Basit Rishi
  • 2,268
  • 24
  • 30
15

I had the same problem. If you try to upload an image that is too large on some low resolution devices, the app will collapse. You can make several images of different sizes (hdpi, xxdpi and more) or simply use an external library to load images that solve the problem quickly and efficiently. I used Glide library (you can use another library like Picasso).

    panel_IMG_back = (ImageView) findViewById(R.id.panel_IMG_back);
    Glide
            .with(this)
            .load(MyViewUtils.getImage(R.drawable.wallpaper)
            .into(panel_IMG_back);
Guy4444
  • 1,411
  • 13
  • 15
10

This issue can be resolved by 3 methods as follows:

Method 1: By adding image into a res/drawable-nodpi folder (By doing this it will not pre-scale your image).

Method 2: Generate all dpi(hdpi,ldpi,mdpi,xhdpi,xxhdpi,xxxhdpi) of image and add to drawable folder. (This process will increase APK size).

Method 3: Add image to drawable/drawable-xxhdpi folder.

All these methods are verified.

elpidaguy
  • 624
  • 1
  • 11
  • 25
Joyal George
  • 420
  • 3
  • 6
9

Turns out the problem was the main image that we used on our app at the time. The actual size of the image was too large, so we compressed it. Then it worked like a charm, no loss in quality and the app ran fine on the emulator.

Francisco
  • 2,050
  • 3
  • 14
  • 19
  • @BekaBot Original question was from a while ago. But if I recall correctly we just ran our image through an online image compressor, and then re-ran our app and then it ran fine. I hope this helps. – Francisco Jul 13 '20 at 02:09
5

For this error was like others said a big image(1800px X 900px) which was in drawable directory, I edited the image and reduced the size proportionally using photoshop and it worked...!!

3

If you don't want your image to be pre-scaled you can move it to the res/drawable-nodpi/ folder.

More info: https://developer.android.com/training/multiscreen/screendensities#DensityConsiderations

Jasper Vergeer
  • 366
  • 3
  • 6
2

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 Glide than Picasso draw to large bitmap

Deniz
  • 319
  • 5
  • 9
2

I just created directory drawable-xhdpi(You can change it according to your need) and copy pasted all the images to that directory.

2

I also had this issue when i was trying to add a splash screen to the android app through the launch_backgrgound.xml . the issue was the resolution. it was too high so the images memory footprint exploded and caused the app to crash hence the reason for this error. so just resize your image using a site called nativescript image builder so i got the ldpi,mdpi and all the rest and it worked fine for me.

0

This can be an issue with Glide. Use this while you are trying to load to many images and some of them are very large:

Glide.load("your image path")
                       .transform(
                               new MultiTransformation<>(
                                       new CenterCrop(),
                                       new RoundedCorners(
                                               holder.imgCompanyLogo.getResources()
                                                       .getDimensionPixelSize(R.dimen._2sdp)
                                       )
                               )
                       )
                       .error(R.drawable.ic_nfs_default)
                       .into(holder.imgCompanyLogo);
           }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Deepak Rajput
  • 731
  • 6
  • 20
0

Try using an xml or a vector asset instead of a jpg or png.

The reason is quite obvious in the exception name itself i.e. the resolution of the resource is too large to render.

You can png to xml using online tools like https://svg2vector.com/ OR add your image to drawable-xxhdpi folder.

Jayant Kapila
  • 67
  • 1
  • 5
0

Solution for Picasso is add Transformation for resize image.

class ResizeTransformation(private val maxSize: Int) : Transformation {
    override fun transform(source: Bitmap?): Bitmap? {
        var result:Bitmap? = null

        if (source != null) {
            var width = source.width
            var height = source.height

            val bitmapRatio = width.toFloat() / height.toFloat()

            if (bitmapRatio > 1) {
                width = maxSize;
                height = (width / bitmapRatio).toInt()
            } else {
                height = maxSize;
                width = (height * bitmapRatio).toInt()
            }

            result = Bitmap.createScaledBitmap(source, width, height, true)
            source.recycle()

        }

        return result
    }


    override fun key() = "resize()"
}

Use:

Picasso.get()
        .load(url)
        .transform(ResizeTransformation(2400)) //FHD+ resolution
        .into(view)
0

Convert your all png formats into webs format. You can do it by Android Studio.

enter image description here

Ahmet B.
  • 1,290
  • 10
  • 20
0

I have the same error message. But I'm downloading the image from the web, so can't move it to a different folder. Using

        AsyncImage(
            model = "https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg?v=47faa659a05e",
            contentDescription = null
        )

which is a link to a very small image. Any idea what's going on? (I'm asking here, because it's the first result, and I can't find anything relevant to my issue)

Arthur Rainbow
  • 216
  • 1
  • 8
0

Maybe your splash screen image is the problem, the size is calculate Height x Width. Try to reduce the image to 720P .

Try to reduce the dimensions of the splash screen.