Android Volley给了我一个outOfMemory异常

时间:2013-06-10 16:59:27

标签: android out-of-memory android-volley

我正在使用凌空从服务器在水平滑动视图中显示图像,但我的图像非常大,这是我得到内存不足的例外

以下是我的排球课程:

public class Volley{

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

public Volley(Context ctx) {
    Log.v("Volley", "Volley onCreate");
    mRequestQueue = com.android.volley.toolbox.Volley.newRequestQueue(ctx);

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
        LruCache<String, Bitmap> imageCache = new LruCache<String, Bitmap>(cacheSize);

        @Override
        public void putBitmap(String key, Bitmap value) {
            imageCache.put(key, value);
        }

        @Override
        public Bitmap getBitmap(String key) {
            return imageCache.get(key);
        }
    };

    mImageLoader = new ImageLoader(mRequestQueue, imageCache);

}

public void clear(Context ctx) {
    mRequestQueue.cancelAll(ctx);
    mImageLoader = null;
    mRequestQueue = null;
}

public RequestQueue getRequestQueue() {
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}}

图片加载器代码:

image.setImageUrl(imagePhoto.url, getVolley(getContext()).getImageLoader());

public Volley getVolley(Context ctx) {
    if(mVolley == null) {
        mVolley = new Volley(getContext());
    }
    return mVolley;
}

例外:

> 06-10 22:14:27.462: E/AndroidRuntime(10060): FATAL EXCEPTION: Thread-29479
06-10 22:14:27.462: E/AndroidRuntime(10060): java.lang.OutOfMemoryError
06-10 22:14:27.462: E/AndroidRuntime(10060):    at com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101)
06-10 22:14:27.462: E/AndroidRuntime(10060):    at com.android.volley.toolbox.PoolingByteArrayOutputStream.<init>(PoolingByteArrayOutputStream.java:53)
06-10 22:14:27.462: E/AndroidRuntime(10060):    at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:202)
06-10 22:14:27.462: E/AndroidRuntime(10060):    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
06-10 22:14:27.462: E/AndroidRuntime(10060):    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

3 个答案:

答案 0 :(得分:5)

我确实解决了这个问题,为LruCache的BitmapLruCache提供了适当的缓存

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public BitmapLruCache() {
    this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

}

这是链接: Android Volley ImageLoader - BitmapLruCache parameter?

答案 1 :(得分:0)

我不知道你的ImageLoader是如何实现的,但它可能会加载一个Bitmap。尝试在生成位图时将以下BitmapOption:inPurgeable设置为true。这样,当内存不足时,将清理位图内存。

看看这个问题:Why would I ever NOT use BitmapFactory's inPurgeable option?

答案 2 :(得分:0)

我不确定您是如何实现滑动视图的。但在大多数实现中(如viewpager),它将缓存3个或更多项。所以你真的不需要提供LRUCache来排球。

只需删除imageCache的代码,您就会看到内存使用情况有所改善。