【问题标题】:android out of memory error after multiple reloads多次重新加载后android内存不足错误
【发布时间】:2016-12-02 18:52:08
【问题描述】:

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

这是我的加载位图类

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;
}

}

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

bitmapHTTP getBitmap = new bitmapHTTP(this);

    getBitmap.loadBitmap(imageUrl, 1), imageView1);
    getBitmap.loadBitmap(imageUrl, 1), imageView2);
    getBitmap.loadBitmap(imageUrl, 1), imageView3);

【问题讨论】:

标签: java android


【解决方案1】:

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

检查您的代码以确保您正确回收。我没有看到任何类似的东西(如果你想做低位图,释放参考是不够的)。

另外,请记住,您需要确保您的位图在可用内存中正确加载并调整大小,以免占用内存。我相信你正在那里的某个地方做这件事。

【讨论】:

  • 是的,图像已正确加载,我的问题是不太确定我需要在哪里回收它们以及如何从我的主类中回收它们
  • 并不是要冒犯您的代码,而是看看这里,它可能会帮助您继续执行其他任务。 github.com/square/picasso
猜你喜欢
  • 1970-01-01
  • 2021-03-12
  • 2017-04-16
  • 2016-08-27
  • 2012-09-21
  • 2012-05-08
  • 2021-10-10
  • 1970-01-01
  • 2013-01-17
相关资源
最近更新 更多