下面是我的凌空课:
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-2947906-10 22:14:27.462: E/AndroIDRuntime(10060): java.lang.OutOfMemoryError06-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)解决方法 我确实解决了这个问题,为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);}@OverrIDeprotected int sizeOf(String key,Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024;}@OverrIDepublic Bitmap getBitmap(String url) { return get(url);}@OverrIDepublic voID putBitmap(String url,Bitmap bitmap) { put(url,bitmap);}
}
链接在这里:
Android Volley ImageLoader – BitmapLruCache parameter?
以上是内存溢出为你收集整理的Android Volley给我一个outOfMemory异常全部内容,希望文章能够帮你解决Android Volley给我一个outOfMemory异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)