Java.Lang.Runtimeexception: Canvas: Trying to Draw Too Large(144000000Bytes) Bitmap

Android Canvas: drawing too large bitmap

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.

Canvas: trying to draw too large bitmap when Android N Display Size set larger than Small

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.

java.lang.RuntimeException: Canvas: trying to draw too large bitmap

Do it progamatically, Try this.

    ImageView icon = new ImageView(this); // Create an icon
icon.setImageDrawable(getResources().getDrawable(R.drawable.add_user));

//set the appropriate background for the main floating action button along with its icon
FloatingActionButton factionButton = new FloatingActionButton.Builder(this)
.setContentView(icon)
.setBackgroundDrawable(R.drawable.selector_floating_button)
.build();

catch RuntimeException: Canvas: trying to draw too large...

The Bitmap is too large in size and the Bitmap object cannot handle it. Thus, ImageView should have the same problem. Solution: resize the image either in programs such as paint.net, or set a fixed size for the bitmap and scale it.

Before I go further, your stacktrace links to the drawing of the bitmap, not creating the object:

at android.graphics.Canvas.drawBitmap(Canvas.java:1415)

As such you can do this:

Bitmap image = BitmapFactory.decodeFile(file.getPath());//loading the large bitmap is fine. 
int w = image.getWidth();//get width
int h = image.getHeight();//get height
int aspRat = w / h;//get aspect ratio
int W = [handle width management here...];//do whatever you want with width. Fixed, screen size, anything
int H = W * aspRat;//set the height based on width and aspect ratio

Bitmap b = Bitmap.createScaledBitmap(image, W, H, false);//scale the bitmap
imageView.setImageBitmap(b);//set the image view
image = null;//save memory on the bitmap called 'image'

Or, as mentioned here, you can use Picasso as well

NOTE

The image you attempted to load in when the stacktrace is from, is 213828900 bytes, which is 213mb. This is probably an image with very high resolution, as the bigger in size they are, the bigger in bytes they are.

With images that big, the method with scaling may not work as it sacrifices too much of the quality. With images that big, Picasso or Glide may be the only options that loads in the pictures without a too big loss of resolution.

Android: Canvas: trying to draw too large

You should read the complete error: Trying to draw too large bitmap. There seems to be a too large image you want to draw. Be aware, that an image, even when it is small as a png or jpeg file, needs to be converted (unpacked) into a bitmap that could become really big.
E.g. a completly white image of 1000x1000 pixels would become a tiny png file, but as a bitmap it would take up 4 million bytes (1000 * 1000 * 4) because 4 bytes per pixel (RGB and Alpha).



Related Topics



Leave a reply



Submit