Java OutOfMemoryError位图android

Java OutOfMemoryError位图android,java,android,caching,bitmap,out-of-memory,Java,Android,Caching,Bitmap,Out Of Memory,我正在android中缓存我的图像,不确定如何按照android的建议重用位图: 这是我的密码 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 L

我正在android中缓存我的图像,不确定如何按照android的建议重用位图: 这是我的密码

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);
在我的任务中

  @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个图像,这可能会耗尽内存,那么如何重用位图或回收未使用的位图呢?我的目标是安卓3.0或更高版本,正如安卓建议的那样,我可以使用Set>mReusableBitmaps;但我不知道如何实现这一点。

处理此问题的最佳方法是实现某种惰性图像加载,在这种情况下,系统可以更轻松地回收弱引用位图。网上有大量的示例,github上有一个非常流行的开源库,可以为您提供所有这些。它们甚至有回调,您可以使用回调在图像加载时显示进度条,并在图像下载完成后将其删除。您可以在这里找到它:

它的工作方式是将图像缓存在设备的硬盘上,然后当它们有一段时间不在视图中时进行回收,然后当再次需要图像时,它出去从设备或internet获取它们。图像是否缓存在设备的某个位置?该库具有不同的配置,因此您可以将其设置为在设备上保存图像,但开箱即用的情况下它不会这样做。我很确定你必须告诉它将图像存储在设备上,当你不这样做时,默认行为是使用默认设置中指定数量的运行时内存,这样你的图像就有可能进入并被回收,因为没有更多的空间。这似乎很有魅力,让我继续测试它是的,很久以前,当android第一次出现的时候,我创造了一个非常相似的东西,它非常棒,现在非常流行,你可以在网上找到开源库和东西,所以你不必一直重新创建轮子:)是的,这绝对是一条路要走。我一直在尝试解决一些定制问题,但仍然非常棒
  @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);


   }