“画布: 试图绘制太大的位图”当 Android N 显示大小设置大于小

我有一个已发布的应用程序,是崩溃在启动 Android N 时,新引入的 Display size操作系统设置为太大的值。

当我查看 logcat 时,我看到以下信息:

java.lang.RuntimeException: Canvas: trying to draw too large(106,975,232 bytes) bitmap.

我在我的第一个活动中追踪到一个 ImageView,它显示了一个漂亮的大背景图像。问题中的图像是2048x1066,位于我的通用 drawables目录中,因此无论密度如何,都将使用这个图像。

Display size设置为 Small时,一切正常。但是当我升到 Default的时候,它就停止工作了。如果我然后交换出一个较小的图像,它在 Default工作,但如果我去到 Large,它停止工作了。

我的猜测是,调整 Display size使您的设备表现得像一个物理上更小的设备,具有更高的像素密度。但我不知道我该怎么做。如果我为了更高的分辨率而逐渐放入更小的图像,那么在实际的大屏幕上看起来就不会那么好了。还是我不明白什么?

如有任何建议,我将不胜感激。

118913 次浏览

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.

Try to use Bitmap.Factory class, this link will help you Loading Large Bitmaps Efficiently

I don't know would it help some one, but I'll just leave it here. In my case - problem was only on Sumsung devices with Android 7, and problem was in splash screen proportions. after changing height to 1024 px - everything works fine

Move your image in the drawable to mipmap-xxhdpi.Your image is in bitmap format so you should put your image in mipmap folder,then it will work

The icon files are too large for Android to efficiently and smoothly load. Android recognizes this with its smart algorithms.

You can resize the icon files using Final Android Resizer by asystat. Resize them to "xhdpi" or lower.

Place the resized photos in drawable or overwrite over the existing large icon files.

Then, you're done.

if you are using glide and you are loading 1k of images at a time or some images then it is issue of glide or whatever you are doing to use to set the image view. you can resolve it just by applying scale type in glide.

I solved the problem after adding the below code into the Manifest file's application tag in between android: lines.

android:hardwareAccelerated="false"

In my case, I just changed the canvas of image which is used in the background using Paint3d(or you can use any other). Here I am sharing a screenshot just go through it.

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

it is solved by resizing the images to a lower size.

There are some scenarios where Original Bitmap needs be Drawn into ImageViews, Photo Editing apps etc...,

as bay mentioned above setting

android:hardwareAccelerated="false"

will Cause bad UI experince, You can set hardwareAccelerated Only one selected Activity where high res image to be drawn

<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>

Need to add Manifest file's application tag in between android: add below lines.

android:hardwareAccelerated="false"

Just an addition to the Johan Franzén's answer, maybe it's a good idea to not only add drawable-xxhdpi density folder, but also add another density folder.
So whatever the android version and size, your app can prepare the image source with the right size :

  1. Change your folder view on the top left from Android to Project
  2. Go to YourProjectFolder folder > app > src > main > res
  3. Prepare the original image with your best resolution, and split it into each folder size automatically. You can do it in Baker
  4. Then create a folder with another density, namely:
  5. drawable-hdpi, drawable-ldpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
  6. Put each image into the appropriate folder

i solved the problem by simply changing the image extension to .png extension, and it worked just fine with me.