I'm using Volley library in my project but I have problem with OutOfMemory Exception. In my application I'm downloading thumbs and full size images from server via NetworkImageView using setImageUrl
method. I'm using 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);
}
}
I'm getting OutOfMemoryException
on HTC Desire (Android 2.2.2). How can I deal with this exception? Is something wrong I'm doing?
Edit
This exception I got during monkey test:
java.lang.OutOfMemoryError at com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101) at com.android.volley.toolbox.PoolingByteArrayOutputStream.expand(PoolingByteArrayOutputStream.java:76) at com.android.volley.toolbox.PoolingByteArrayOutputStream.write(PoolingByteArrayOutputStream.java:84) at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:213) at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
@Sipka - it doesn't solve my problem
@Muhammad Babar - Volley library handles all network/bitmap/cache operations so I need solution to fix OutOfMemory exception caused by Volley.
Use this code to create Bitmap in a thread that will help you
}