不加载从SD每次一个图像(使用WeakReference的?) [英] Not loading an image from sd everytime (use WeakReference?)

查看:150
本文介绍了不加载从SD每次一个图像(使用WeakReference的?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种人名单,每个人都有一个化身。有每个人的头像默认以后你可以改变它(应用程序保存调整大小和圆裁剪图像到SD)。

I have this list of people and each person has an avatar. There's a default avatar for everyone and later you can change it (the app saves the resized and circle-cropped image to the sd).

下面是我的方法:

 <ImageView
        android:id="@+id/photo_list_item"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:layout_marginRight="16dip"
        android:contentDescription="@string/avatar_descp"
        android:src="@drawable/defavatar" >
    </ImageView>

这是形成列表中的项目的一部分。 defavatar 是我已经谈到了默认的头像。然后:

This is part of the item that forms the list. defavataris the default avatar I already talked about. Then:

String m = person.getAvatar();
    if(!m.equals(""))
    {
        loadBitmap("file://"+m, avatar);
    }

getAvatar()得到DB的化身的路径。如果不为null:

getAvatar()gets the path of the avatar of the db. If not null:

public void loadBitmap(String resId, ImageView imageView)
{
    BitmapWorkerTask task = new BitmapWorkerTask(imageView);
    task.execute(resId);
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView)
    {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params)
    {
        return decodeSampledBitmapFromResource(getContext(), Uri.parse(params[0]), 320, 320);
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap)
    {
        if (imageViewReference != null && bitmap != null)
        {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null)
            {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

这code大多是从官方Android开发人员指南复制。

This code is copied mostly from the official Android Dev Guides.

我的问题是这样的:每次加载图像,一遍又一遍(也是我最先看到的默认头像和一些毫秒后新的头像代替,这是不是很漂亮)。

My problem is this: Everytime the image is loaded, again and again (and also I see first the default avatar and some ms later the new avatar instead, and this is not very nice).

我猜,我可以使用的WeakReference(这是图像的高速缓存,是吧?)保存在内存中,直到内存泄漏。我对吗?我该怎么办?

I'm guessing that I can use the WeakReference (which is a cache of the image, right?) that holds in the memory until a memory leak. Am I right? What can I do?

推荐答案

不要使用的WeakReference 。使用 LruCache 来代替。 Android的垃圾收集器是非常积极和内存密集型图像弱引用会被垃圾收集器很快被清除。从谷歌:

Do not use WeakReference. Use an LruCache instead. Android's garbage collector is very aggressive and weak references to memory-intensive images would be cleared by the garbage collector very quickly. From Google:

在过去,一个流行的内存缓存的实现是一个SoftReference的
  或WeakReference的位图缓存,但不建议这样做。
  从Android 2.3的启动(API等级9)垃圾收集更
  积极与收集软/弱引用这使得他们
  相当无效的。

In the past, a popular memory cache implementation was a SoftReference or WeakReference bitmap cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective.

有关使用帮助 LruCache ,看到此链接:缓存位图

For help on using LruCache, see this link: Caching Bitmaps.

此外,作为@Sainath建议,使用第三方加载图像库,而不是。有很多在Android中处理图像加载时要考虑的,所以最好使用已经采取所有这些因素考虑进去库。

Also, as @Sainath suggested, use a third-party image loading library instead. There is a lot to consider when handling image loading in Android, so it's best to use a library that already takes all those factors into consideration.

这篇关于不加载从SD每次一个图像(使用WeakReference的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆