【问题标题】:Bitmap image avoid out of memory with large amount of image views位图图像避免大量图像视图内存不足
【发布时间】:2012-07-03 16:22:15
【问题描述】:

我认为,标题是如此具体。所以,我想先解释一下,我想创建一个图章应用程序(如 photobox),我们可以在照片上放置大量图章图像,保存图像和设计。

我现在在我的代码中使用 option.inSampleSize。更有可能是这个链接:Strange out of memory issue while loading an image to a Bitmap object

当图像数量较小时,这解决了我的问题。但是当图像数量(图像计数)变得更大时,内存再次存在。

这里的专家有什么想法吗?目前我仍在使用 ImageView 来显示图章图像。

或者..也许还没有解决方案?我唯一想到的就是限制图像的数量。

【问题讨论】:

  • “我唯一想到的就是限制图像的数量。”听起来像。

标签: android image memory


【解决方案1】:

您是否按照 Google 的图片缓存教程进行操作?它概述了陷阱并向您展示了如何正确缓存图像,以免超出内存预算。

Android Developer: Displaying Bitmaps - Cache Bitmap

关键线是

// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
        Context.ACTIVITY_SERVICE)).getMemoryClass();

// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;

mMemoryCache = new LruCache(cacheSize) {
    @Override
    protected int sizeOf(String key, Bitmap bitmap) {
        // The cache size will be measured in bytes rather than number of items.
        return bitmap.getByteCount();
    }
};

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 2013-12-20
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 2012-12-23
    相关资源
    最近更新 更多