Android的排球给我一个内存不足异常 [英] Android Volley give me an outOfMemory exception

查看:285
本文介绍了Android的排球给我一个内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的凌空显示从服务器横向轻扫浏览图片,但我的图像是相当大的那是方式,我得到一个异常内存不足

下面是我的截击类:

 公共类凌空{

私人请求队列mRequestQueue;
私人ImageLoader的mImageLoader;

公共排球(上下文CTX){
    Log.v(排球,凌空的onCreate);
    mRequestQueue = com.android.volley.toolbox.Volley.newRequestQueue(CTX);

    最终诠释maxMemory =(INT)(调用Runtime.getRuntime()maxMemory()/ 1024);
    最终诠释CACHESIZE = maxMemory / 8;

    ImageLoader.ImageCache imageCache =新ImageLoader.ImageCache(){
        LruCache<字符串,位图> imageCache =新LruCache<字符串,位图>(CACHESIZE);

        @覆盖
        公共无效putBitmap(字符串键,位图值){
            imageCache.put(键,值);
        }

        @覆盖
        公共位图getBitmap(字符串键){
            返回imageCache.get(键);
        }
    };

    mImageLoader =新ImageLoader的(mRequestQueue,imageCache);

}

公共无效清除(上下文CTX){
    mRequestQueue.cancelAll(CTX);
    mImageLoader = NULL;
    mRequestQueue = NULL;
}

公共请求队列getRequestQueue(){
    返回mRequestQueue;
}

公共ImageLoader的getImageLoader(){
    返回mImageLoader;
}}
 

图片装载机code:

(imagePhoto.url,get​​Volley(的getContext())getImageLoader())

  image.setImageUrl;

公共排球getVolley(上下文CTX){
    如果(mVolley == NULL){
        mVolley =新的排球(的getContext());
    }
    返回mVolley;
}
 

异常:

 > 06-10 22:14:27.462:E / AndroidRuntime(10060):致命异常:线程29479
06-10 22:14:27.462:E / AndroidRuntime(10060):java.lang.OutOfMemoryError
06-10 22:14:27.462:E / AndroidRuntime(10060):在com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101)
06-10 22:14:27.462:E / AndroidRuntime(10060):在com.android.volley.toolbox.PoolingByteArrayOutputStream.<init>(PoolingByteArrayOutputStream.java:53)
06-10 22:14:27.462:E / AndroidRuntime(10060):在com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:202)
06-10 22:14:27.462:E / AndroidRuntime(10060):在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
06-10 22:14:27.462:E / AndroidRuntime(10060):在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
 

解决方案

我没有解决这个问题,提供适当的高速缓存中的BitmapLruCache insted的LruCache的

 公共类BitmapLruCache扩展LruCache&LT;字符串,位图&GT;实现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,位图);
}
 

}

这里是链接: <一href="http://stackoverflow.com/questions/16682595/android-volley-imageloader-bitmaplrucache-parameter">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;
}

Exception:

> 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?

这篇关于Android的排球给我一个内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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