gpt4 book ai didi

Android:BitmapFactory.decodeStream() 内存不足,400KB 文件和 2MB 可用堆

转载 作者:IT老高 更新时间:2023-10-28 13:17:07 31 4
gpt4 key购买 nike

我的应用在源代码中的以下行遇到 OOM 错误:

image = BitmapFactory.decodeStream(assetManager.open(imgFilename));

就在导致应用因 OOM 错误而终止的分配之前:

(...)
08-05 21:22:12.443: I/dalvikvm-heap(2319): Clamp target GC heap from 25.056MB to 24.000MB
08-05 21:22:12.443: D/dalvikvm(2319): GC_FOR_MALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 58ms
08-05 21:22:14.513: D/dalvikvm(2319): GC_EXTERNAL_ALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 101ms
08-05 21:22:14.903: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:22:14.903: D/dalvikvm(2319): GC_FOR_MALLOC freed 0K, 50% free 2709K/5379K, external 18312K/19336K, paused 53ms
08-05 21:22:22.843: D/ddm-heap(2319): Heap GC request
08-05 21:22:22.963: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:22:22.963: D/dalvikvm(2319): threadid=1: still suspended after undo (sc=1 dc=1)
08-05 21:22:22.963: D/dalvikvm(2319): GC_EXPLICIT freed 1K, 50% free 2710K/5379K, external 18312K/19336K, paused 116ms

DDMS 报告了关于堆状态的类似图片:

Heap Size:  5.254 MB
Allocated: 2.647 MB
Free: 2.607 MB
%Used: 50.38%
#Objects 49,028

单步越过此行会导致 OOM 错误:

08-05 21:26:04.783: D/dalvikvm(2319): GC_EXTERNAL_ALLOC freed <1K, 50% free 2710K/5379K, external 18312K/19336K, paused 57ms
08-05 21:26:05.023: E/dalvikvm-heap(2319): 2097152-byte external allocation too large for this process.
08-05 21:26:05.163: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:26:05.163: E/GraphicsJNI(2319): VM won't let us allocate 2097152 bytes
08-05 21:26:05.163: D/dalvikvm(2319): GC_FOR_MALLOC freed 0K, 50% free 2710K/5379K, external 18312K/19336K, paused 30ms
08-05 21:26:05.283: D/skia(2319): --- decoder->decode returned false
  1. “imgFileName”引用的文件大小在 Windows 上报告为 < 400K。那么为什么 BitmapFactory.decodeStream 会尝试分配 2MB 呢?
  2. 为什么似乎有足够的可用空间时会出现 OOM 错误?

此应用面向 Android 2.2 及更高版本。

提前致谢!

最佳答案

Android 库在加载图像方面并不那么聪明,因此您必须为此创建解决方法。

在我的测试中,Drawable.createFromStreamBitmapFactory.decodeStream 使用更多的内存。

您可以更改配色方案以减少内存 (RGB_565),但图像也会降低质量:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);

引用:http://developer.android.com/reference/android/graphics/Bitmap.Config.html

您还可以加载缩放的图像,这将大大减少内存使用量,但您必须知道您的图像不会损失太多质量。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);

引用:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

要动态定义 inSampleSize,您可能想知道图像大小以做出决定:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(stream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

options.inJustDecodeBounds = false;
// recreate the stream
// make some calculation to define inSampleSize
options.inSampleSize = ?;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);

您可以根据设备的屏幕尺寸自定义 inSampleSize。要获取屏幕大小,您可以执行以下操作:

DisplayMetrics metrics = new DisplayMetrics();
((Activity) activity).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;

其他教程:

关于Android:BitmapFactory.decodeStream() 内存不足,400KB 文件和 2MB 可用堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820266/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com