WVGA800 上的 Android OutOfMemoryError,而不是任何小屏幕格式

发布于 2024-12-06 07:20:42 字数 249 浏览 0 评论 0原文

当我尝试在运行低于 WVGA800 的模拟器上解码位图时,它工作正常(包括手机),但在较大的屏幕上,它会抛出 OutOfMemoryError

为什么会这样?屏幕越大的手机内存会越大吗?

private Bitmap getBitmap(int assetKey) { return BitmapFactory.decodeResource(mContext.getResources(), assetKey); }

When I try and decode a bitmap on an emulator running less than WVGA800 it works fine (phones included) but on larger screens it throws a OutOfMemoryError

Why would that be? would phones with larger screens have more memory?

private Bitmap getBitmap(int assetKey) { return BitmapFactory.decodeResource(mContext.getResources(), assetKey); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2024-12-13 07:20:42

显示屏较大的手机并不总是比显示屏较小的手机拥有更多的内存。解码后的位图需要大量内存,每个像素需要 4 字节内存。

一般来说,如果位图太大,则最好对位图进行下采样。您可以轻松地做到这一点:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = n; // <-- this only decode every nth pixel

Bitmap b = BitmapFactory.decodeResource(mContext, rId, ops);

Phones with larger displays don't always have more memory than phones will smaller displas. Decoded bitmaps take a lot of of memory, 4 bytes of memory per pixel.

In general it is a good idea to downsample the bitmap if they are too large. You can do this easily:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = n; // <-- this only decode every nth pixel

Bitmap b = BitmapFactory.decodeResource(mContext, rId, ops);
梦开始←不甜 2024-12-13 07:20:42

通常,将数据分解为样本是一个很好的做法,以避免内存异常。您可以查看以下链接。你必须使用大于 1 的sampleSize。我已经按照这篇文章解决了我的问题。

将图像加载到 Bitmap 对象时出现奇怪的内存不足问题

It's generally a good practice to break the data in to samples, to avoid memory exception. you can see the following link for that. you have to use sampleSize more than 1. I have resolved my issues by following this post.

Strange out of memory issue while loading an image to a Bitmap object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文