Android - 如何使用位图避免内存过载?

发布于 2024-10-17 01:21:43 字数 385 浏览 3 评论 0原文

我的应用程序使用位图,每次用户进入特定活动时,它都会在第二次停止工作时显示图像。

Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"Image.jpg");

我尝试过使用诸如...之类的东西,

BitmapFactory.Options options = new BitmapFactory.Options();
     options.inTempStorage = new byte[16*1024];

也不知道要设置什么。但这没有帮助。一旦用户离开此活动,是否没有办法清除位图等?谢谢

My application is using bitmaps and every time the user come to the specific activity where it shows an image the second time it stops working.

Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"Image.jpg");

I have tried using things like...

BitmapFactory.Options options = new BitmapFactory.Options();
     options.inTempStorage = new byte[16*1024];

Not sure what to set it too. But this doesnt help. Once the user leaves this activity is there not a way to clear the bitmap etc? thanks

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

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

发布评论

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

评论(3

晨曦÷微暖 2024-10-24 01:21:43

调用 Bitmap.recycle()使用位图释放内存完成。

Call Bitmap.recycle() when you are done using the Bitmap to free the memory.

羁客 2024-10-24 01:21:43

除了按照建议使用 Bitmap.recycle() 之外(这并不适合所有情况,而且问“我还需要这个位图吗?”是一件令人头疼的事情),我总是使用这种效果很好的技术:

// 1. create a cache map
private WeakHashMap<String, SoftReference<Bitmap>> mCache;

正如你可以的看,它是 WeakReference 带有 SoftReference作为价值观。

//2. when you need a bitmap, ask for it:
public Bitmap get(String key){
    if( key == null ){
        return null;
    }
    if( mCache.containsKey(key) ){
        SoftReference<Bitmap> reference = mCache.get(key);
        Bitmap bitmap = reference.get();
        if( bitmap != null ){
            return bitmap;
        }
        return decodeFile(key);
    }
    // the key does not exists so it could be that the
    // file is not downloaded or decoded yet...
    File file = new File(Environment.getExternalStorageDirectory(), key);
    if( file.exists() ){
        return decodeFile(key);
    } else{
        throw new RuntimeException("Boooom!");
    }
}

这将检查缓存映射。如果文件已经解码,则返回;否则它将被解码并缓存。

//3. the decode file will look like this in your case
private Bitmap decodeFile(String key) {
    Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"+key);
    mCache.put(key, new SoftReference<Bitmap>(bitmap));
    return bitmap;
}

使用软引用很好,因为您将从内存中删除位图的责任转移到了操作系统。

Besides using Bitmap.recycle() as suggested (which is not suitable for all situations and it's a pain in the neck to be asking: "do I still need this bitmap?"), I always use this technique which works really fine:

// 1. create a cache map
private WeakHashMap<String, SoftReference<Bitmap>> mCache;

As you can see, it's a hash map of WeakReferences with a SoftReference as the values.

//2. when you need a bitmap, ask for it:
public Bitmap get(String key){
    if( key == null ){
        return null;
    }
    if( mCache.containsKey(key) ){
        SoftReference<Bitmap> reference = mCache.get(key);
        Bitmap bitmap = reference.get();
        if( bitmap != null ){
            return bitmap;
        }
        return decodeFile(key);
    }
    // the key does not exists so it could be that the
    // file is not downloaded or decoded yet...
    File file = new File(Environment.getExternalStorageDirectory(), key);
    if( file.exists() ){
        return decodeFile(key);
    } else{
        throw new RuntimeException("Boooom!");
    }
}

This will check the cache map. If the file was already decoded, it will be returned; otherwise it will be decoded and cached.

//3. the decode file will look like this in your case
private Bitmap decodeFile(String key) {
    Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"+key);
    mCache.put(key, new SoftReference<Bitmap>(bitmap));
    return bitmap;
}

Working with soft references is nice because you shift the responsibility of removing bitmaps from memory to the OS.

清秋悲枫 2024-10-24 01:21:43

请注意。
当我们考虑软引用时,我们认为操作系统会在报告内存不足异常之前从内存中删除软引用对象。

在 Android 中,情况并非总是如此。我必须实现自己的图像缓存系统,并且我可以向您保证,当内存几乎已满时,软引用对象不会从内存中删除。

最后,我不得不切换到硬引用(普通引用),但使用 android.support.v4.util.LruCache 来管理缓存的对象。我会从 lru 缓存的 onRemoved 回调中调用回收。它绝对更方便。

干杯。

Be aware.
When we think about softreferences we think that the OS will remove the softreferenced objects from memrory before reporting an outofmemory exception.

In android this is not always true. I had to implement my own caching system for images and I can assure you softreferenced objects were not removed from memory when memory was almost full.

Finally I had to switch to hard references (the normal ones) but used android.support.v4.util.LruCache for managing the cached objects. I would call recycle on the onRemoved callback from the lru cache. Its definetely more convenient.

Cheers.

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