OutOfMemoryError位图android

时间:2013-11-27 19:50:47

标签: java android caching bitmap out-of-memory

我在android中缓存我的图像,不知道如何在android建议重用位图: https://developer.android.com/training/displaying-bitmaps/manage-memory.html 这是我的代码

final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;
    this.imageCache= 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;
            }

    };
    this.m_adapter = new ImageScreenAdapter(this, R.layout.imagelist_item, items, imageCache);
    setListAdapter(this.m_adapter);

这是我用来下载我的位图的方法

  private Bitmap downloadBitmap(String url, ProgressBar progress, int position) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);
    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode
                    + " while retrieving bitmap from " + url);
            if(progress!=null)
            {
              RemoveImageResults(position);
              return null;
            }
            return BitmapFactory.decodeResource(getResources(), R.drawable.missingpic);
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = false;
                options.inDither = true;
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream,null, options);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or
        // IllegalStateException
        getRequest.abort();
        //Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}

和我的AsyncTask

  @Override
   protected void onPostExecute(Bitmap result) {
      final Bitmap Image=result;
       if(Image!=null)
           imageCache.put(imageUrl, Image);
       myActivity.runOnUiThread(new Runnable() {
           public void run() {

               imageView.setImageBitmap(Image);
               imageView.setVisibility(View.VISIBLE);

           }
         });

   }

   private Bitmap download_Image(String url) {
      return downloadBitmap(url, progress, position);


   }

但是如果我的列表适配器中有1000个图像,这可能会耗尽内存,那么如何重用位图或回收未使用的位图呢?我的目标是Android 3.0或更好,因为Android建议我可以使用Set&gt; mReusableBitmaps;但我不遵循如何实现这一点。

1 个答案:

答案 0 :(得分:1)

处理此问题的最佳方法是实现某种延迟图像加载,其中您具有弱引用的位图,系统可以更容易地回收这些位图。在线有大量样本,github上有一个非常流行的开源库,可以为您完成所有这些。它们甚至还有回调,您可以在图像加载时显示进度条,并在下载图像时将其删除。您可以在此处找到它:https://github.com/nostra13/Android-Universal-Image-Loader