recycleview网络映像导致内存泄漏?

时间:2016-09-13 21:17:18

标签: android bitmap android-bitmap bitmapfactory

我正在使用 NetworkImageView 来显示我的图片。 在RecycleView适配器中,我使用imageloader和LruBitmapCache设置图像, 但我遇到内存泄漏如下,任何想法如何解决?我读过其他解决方案是包括" android:largeHeap =" true" "在清单中,但这不能解决问题但只能延迟问题。

java.lang.OutOfMemoryError: Failed to allocate a 52428812 byte allocation with 33553680 free bytes and 48MB until OOM
                           at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                           at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                           at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                           at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)

我以这种方式设置图像:

ImageLoader mImageLoader=MySingleton.getInstance(mcontext).getImageLoader();
 contactViewHolder.imageView.setImageUrl(thumbnail,mImageLoader);


public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
                LruBitmapCache.getCacheSize(mCtx)));


    }
    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}



public class LruBitmapCache extends LruCache<String, Bitmap>
        implements ImageLoader.ImageCache {

    public LruBitmapCache(int maxSize) {
        super(maxSize);
    }

    public LruBitmapCache(Context ctx) {
        this(getCacheSize(ctx));
    }

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

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

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

    // Returns a cache size equal to approximately three screens worth of images.
    public static int getCacheSize(Context ctx) {
        final DisplayMetrics displayMetrics = ctx.getResources().
                getDisplayMetrics();
        final int screenWidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;
        // 4 bytes per pixel
        final int screenBytes = screenWidth * screenHeight * 4;

        return screenBytes * 3;
    }
}

0 个答案:

没有答案