多次重载后android内存不足错误

时间:2016-12-02 18:52:09

标签: java android

嘿伙计们多次重新加载活动后遇到问题我得到了一个内存不足异常并且崩溃了应用程序我正在使用开发人员谷歌文档示例加载多个图像从我看到我必须回收位图一次我完成了它,但我有点不确定在哪里这样做,考虑到我有多个位图并从外部类调用位图

这是我的加载位图类

# help from http://stackoverflow.com/a/28463266/3393459
# and http://stackoverflow.com/a/2846697/3393459


from multiprocessing.dummy import Pool as ThreadPool 
import requests
import pandas as pd


pool = ThreadPool(4) 

# called by each thread
def get_web_data(url):
    return {'col1': 'something', 'request_data': requests.get(url).text}


urls = ["http://google.com", "http://yahoo.com"]
results = pool.map(get_web_data, urls)


print results
print pd.DataFrame(results)

这就是我调用位图加载到imageView的方法

public class bitmapHTTP{

Context ctx;
private LruCache<String, Bitmap> mMemoryCache;
int iv;

public  bitmapHTTP(Context c){
    ctx = c;
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };

}

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if(getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

public void loadBitmap(String resId, ImageView imageView) {
    final String imageKey = String.valueOf(resId);
    iv = imageView.getWidth();
    Bitmap bitmap = getBitmapFromMemCache(imageKey);
    if(bitmap != null) {
        imageView.setImageBitmap(bitmap);
    }else{
        if(cancelPotentialWork(imageKey, imageView)){
            Bitmap loading = decodeSampledBitmapFromResource(ctx.getResources(), R.drawable.whiteseetrough, imageView.getWidth(), imageView.getWidth());
            BitmapWorkerTask task = new BitmapWorkerTask(imageView);
            AsyncDrawable asyncDrawable = new AsyncDrawable(ctx.getResources(), loading, task);
            imageView.setImageDrawable(asyncDrawable);
            task.execute(resId);
        }
    }
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private String data = null;

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params) {
        data = params[0];
        final BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap;

        if(data.equals("null")){
            bitmap = decodeSampledBitmapFromResource(ctx.getResources(), R.drawable.whiteseetrough, iv, iv);
            addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
            return bitmap;
        }else{
            bitmap = getBitmapFromMemCache(data);
            if(bitmap == null){
                // Process as normal
                try {
                    URL url = new URL(data);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();

                    options.inSampleSize = calculateInSampleSize(options, iv, iv);

                    options.inJustDecodeBounds = false;
                    bitmap = BitmapFactory.decodeStream(input, null, options);
                    addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
                    return bitmap;

                }catch(IOException e){
                    e.printStackTrace();
                    return null;
                }
            }

        }
        return bitmap;
    }

    // Once complete, see if ImageView is still around and set bitmap.

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            final BitmapWorkerTask bitmapWorkerTask =
                    getBitmapWorkerTask(imageView);
            if (this == bitmapWorkerTask && imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }

}

static class AsyncDrawable extends BitmapDrawable {
    private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

    public AsyncDrawable(Resources res, Bitmap bitmap,
                         BitmapWorkerTask bitmapWorkerTask) {
        super(res, bitmap);
        bitmapWorkerTaskReference =
                new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
    }

    public BitmapWorkerTask getBitmapWorkerTask() {
        return bitmapWorkerTaskReference.get();
    }
}

public static boolean cancelPotentialWork(String data, ImageView imageView) {
    final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);

    if (bitmapWorkerTask != null) {
        final String bitmapData = bitmapWorkerTask.data;
        // If bitmapData is not yet set or it differs from the new data
        if(bitmapData == null || bitmapData != data) {
            // Cancel previous task
            bitmapWorkerTask.cancel(true);
        } else {
            // The same work is already in progress
            return false;
        }
    }
    // No task associated with the ImageView, or an existing task was cancelled
    return true;
}

private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
    if (imageView != null) {
        final Drawable drawable = imageView.getDrawable();
        if (drawable instanceof AsyncDrawable) {
            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
            return asyncDrawable.getBitmapWorkerTask();
        }
    }
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

这就是很多代码。我很快就会问:你是否在不需要它们后回收你的位图?位图需要回收并释放RAM,特别是在通过循环等加载它们并保持对它们的引用时。

检查您的代码以确保正确回收。我没有看到任何类型的东西(如果你想做一个低位图,那么释放引用是不够的。)

另外,请记住,您需要确保将位图正确加载到可用内存中并调整大小以便它们不会终止内存。我相信你是在那里的某个地方做的。