Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...