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

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

catch thesaurus
catch verb
there is a catch meaning
catch up
catch past tense
catch sentence
how to pronounce catch
catch as a noun in a sentence

I have an application which draws an image from the file system to screen like so:

Bitmap image = BitmapFactory.decodeFile(file.getPath());
imageView.setImageBitmap(image);

If the image is very large I see this error:

java.lang.RuntimeException: Canvas: trying to draw too large(213828900bytes) bitmap.
    at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
    at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
    ...

The stack does not reach my code. How can I catch this error? Or is there a more appropriate way to be drawing the image to the imageView that can avoid this error?


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.

What does 'you are a catch” mean?, be lucky to have them around. It's usually used between friends or acquantances to describe the other's partner and to establish that they believe their partner is attractive due to their looks or personality. Define catch. catch synonyms, catch pronunciation, catch translation, English dictionary definition of catch. seize or capture; trap or ensnare; contract, as a cold


Any app allow user pick picture as background or draw somewhere, it must encount the problem, if user pickup a panoramic photograph or big picture your app must force close, and YOU HAVE NO CHANCE TO PREVENT THE MISTACK, because you can't catch the Exception.

Ok, I just need catch the Exception and tell user we can't load the picture then finish, it too redundant to use Picasso or such library for the additional function, android coding is suffer and I don't want make it more suffer.

finally, I found the code in core.java.android.view.DisplayListCanvas, there is define a max image size with variable MAX_BITMAP_SIZE

private static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024; // 100 MB

You couldn't read the variable in your program, it's define as private(but if you have any way to read the variable please let me know), and here is the part code that throw RuntimeException:

int bitmapSize = bitmap.getByteCount();
if (bitmapSize > MAX_BITMAP_SIZE) {
    throw new RuntimeException(
        "Canvas: trying to draw too large(" + bitmapSize + "bytes) bitmap.");
    }

I just copy the part above, check the size of bitmap in my code, if it over 100M then show message to user the message I mention above then finish.

If your case is the same with me, hope it help you.

You gonna make me catch a body if you ain't careful, noun. the act of catching. anything that catches, especially a device for checking motion, as a  Catch definition, to seize or capture, especially after pursuit: to catch a criminal; to catch a runaway horse. See more.


You should check the size of image and load smaller size of image to avoid exceptions. Please read this article: Loading Large Bitmaps Efficiently

You can also use Picasso Library

there is a catch, catch definition: 1. to take hold of something, especially something that is moving through the air: 2. to find and…. Learn more. catch 1. verb To see, and perhaps apprehend, someone in the act of doing something (often something nefarious). The robbers were arrested when the police caught them looting another house. I don't watch to catch you kids doing this ever again—the tool shed is far too dangerous to play in!


I fixed the error in LunarWatcher's code.

Bitmap image = BitmapFactory.decodeFile(file.getPath());
float w = image.getWidth();//get width
float h = image.getHeight();//get height
int W = [handle width management here...];
int H = (int) ( (h*W)/w);
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'

catch | Free On-Line English Dictionary | Thesaurus, Definition of catch. (Entry 1 of 2). transitive verb. 1a : to capture  Catch is the first personal benefits platform built for freelancers, contractors, gigsters, part-time, and other non-traditional workers. The full suite: taxes, time off, retirement, and health Set aside part of each paycheck to ensure you're covered today and for the future. A plan that goes wherever you do


Catch, What is the definition of catch? What is the meaning of catch? How do you use catch in a sentence? What are synonyms for catch? Synonyms for catch at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for catch.


CATCH, (uncountable) The game of catching a ball. The kids love to play catch. (​countable) Something which is captured or caught. The  For over 25 years, the CATCH platform has been proven to launch kids and communities toward healthier lifestyles. Originators of “GO, SLOW and WHOA” food categories, CATCH is the most cost effective means of preventing childhood obesity, in an environment that’s fun and easy to sustain. Learn More at CATCHinfo.org


Catch, catch. (kætʃ). vb, catches, catching or caught. 1. (tr) to take hold of so as to retain or restrain  CATCH 'CATCH' is a 5 letter word starting with C and ending with H Crossword clues for 'CATCH'