Francisco
Francisco

Reputation: 2228

Android Canvas: drawing too large bitmap

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.

Upvotes: 162

Views: 223615

Answers (19)

Amoko Ivan
Amoko Ivan

Reputation: 11

Just resized your image dimensions to less than 720px using this website https://www.iloveimg.com/resize-image#resize-options,pixels

Upvotes: 0

Zafer ATLI
Zafer ATLI

Reputation: 81

Its generally related with drawable folder,

Android OS try to scale your drawable folder's image with different size. When you are not specified drawable-ldpi, drawable-hdpi, drawable-xhdpi etc. Android OS trying to resize your image but sometimes its goes to wrong and end up canvas drawing too large bitmap error link-1 link-2.

Solution is the specify drawable sizes so prevent to Android OS's resize process. And also Hardware acceleration is a double-edged sword, you need to use very carefully, because according Android documantation GPU texture not suitable for animations when you try so often to modify bitmaps, shapes, objects its causing high resource usage.

As a solution, my recommendations:

Problem - 1: Unspefiy drawable folder cause resize process, and its gone wrong

Solutions:

Solution - 1-1: You can disabled resize process whole app, but its could be create layout issues:

adding this code to androidManifest

 <supports-screens
android:anyDensity="false"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />

Solution - 1-2: Disable drawable resize from folder

Change your drawable folder's name to drawable-nodpi

Android size and inch's very complicated, two phones may have same inch but not same size, pixel count not statically set by inch this situation called aka Pixel density, you can find detailed documantation so its may create layout issues with different devices.

Solution - 1-3: Create different drawable folders [Recomanded]

Create:

drawable-ldpi drawable-mdpi drawable-hdpi drawable-xhdpi drawable-xxhdpi

folders and add image according this documantation with this solution Android OS gona stop resizing images - and its the probably source of this canvas drawing too large bitmap and shows size matters images.


Problem - 2: hardwareAccelerated properly usage you can read all detailed information from android documantation and there debate about to use hardwareAccelerated to use or not use and its seems to problem for rendering complex animation included screen so disabled to this feature may work.

Solution - 2 - 1:

in AndroidManifest

    <application
       ....
       android:hardwareAccelerated="false"
       ...

Performance Section


hardwareAccelerated Perforamance differencies when false:

Performance Summary: hardwareAccelerated:true hardwareAccelerated:false

Ram: True False

FPS: True False

CPU: True False


largeHeap Performance Differencies:

Summary: largeHeap:true largeHeap:false

RAM: largeHeap:true largeHeap:false

CPU: largeHeap:true largeHeap:false

UI: largeHeap:true largeHeap:false

Upvotes: 2

mujeeb
mujeeb

Reputation: 237

File -> Invalidate caches, works for me

Upvotes: -1

Alexander Ferreras
Alexander Ferreras

Reputation: 1698

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.

Upvotes: -1

Ahmet B.
Ahmet B.

Reputation: 1674

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

enter image description here

Upvotes: -1

Hadym Varan
Hadym Varan

Reputation: 39

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)

Upvotes: 0

Joyal George
Joyal George

Reputation: 590

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.

Upvotes: 16

Jayant Kapila
Jayant Kapila

Reputation: 87

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.

Upvotes: 0

Jasper Vergeer
Jasper Vergeer

Reputation: 396

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

Upvotes: 3

Abdul Basit Rishi
Abdul Basit Rishi

Reputation: 2425

This solution worked for me.

Add these lines in your Manifest application tag

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

    

Upvotes: 41

williamjnrkdd
williamjnrkdd

Reputation: 49

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.

Upvotes: 3

Nace
Nace

Reputation: 3174

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.

Upvotes: 57

Sourabh Chaudhary
Sourabh Chaudhary

Reputation: 31

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

Upvotes: 2

Deniz
Deniz

Reputation: 325

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

Upvotes: 2

Deepak Rajput
Deepak Rajput

Reputation: 761

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);
           }

Upvotes: 0

Guy4444
Guy4444

Reputation: 1649

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);

Upvotes: 15

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...!!

Upvotes: 6

Md Imran Choudhury
Md Imran Choudhury

Reputation: 9997

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.

Upvotes: 294

Francisco
Francisco

Reputation: 2228

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.

Upvotes: 10

Related Questions