更新时间:2023-01-14 14:08:04
我没有解决这个问题,提供适当的高速缓存中的BitmapLruCache insted的LruCache的
公共类BitmapLruCache扩展LruCache<字符串,位图>实现ImageLoader.ImageCache {
公共静态INT getDefaultLruCacheSize(){
最终诠释maxMemory =(INT)(调用Runtime.getRuntime()maxMemory()/ 1024);
最终诠释CACHESIZE = maxMemory / 8;
返回CACHESIZE;
}
公共BitmapLruCache(){
这个(getDefaultLruCacheSize());
}
公共BitmapLruCache(INT sizeInKiloBytes){
超(sizeInKiloBytes);
}
@覆盖
保护INT整型尺寸(字符串键,位图值){
返回value.getRowBytes()* value.getHeight()/ 1024;
}
@覆盖
公共位图getBitmap(字符串URL){
得到的回报(URL);
}
@覆盖
公共无效putBitmap(字符串URL,位图位图){
把(URL,位图);
}
}
这里是链接: Android排球ImageLoader的 - BitmapLruCache参数
I am using volley to showing images in horizontal swipe view from the server, but my images are quite large that's way i am getting an exception of out of memory
Below is my volley class:
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 loader code:
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)
i did fix this to provide proper cache to the BitmapLruCache insted of LruCache
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);
}
}
here is the link: Android Volley ImageLoader - BitmapLruCache parameter?