0

您好我正在创建一个android应用程序,并且我在我的网站上列出了超过1500张照片,我想使用阵列适配器在ListView上显示 我找到了一个解决方案正在从我有的URL链接下载图像。 这里是堆栈跟踪当从网站链接url检索图像时出现内存不足异常

我的问题是,我得到一个内存溢出异常

:E/AndroidRuntime(1123):致命异常:螺纹79 :E/AndroidRuntime(1123) :过程: android.quotations,PID:1123 **:E/AndroidRuntime(1123):java.lang.OutOfMemoryError:E/AndroidRuntime(1123): 在** android.graphics.BitmapFactory.nativeDecodeStream(母语方法) :E/AndroidRuntime(1123):在 android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613) :E/AndroidRuntime(1123):在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589) :E/AndroidRuntime(1123):在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422) :E/AndroidRuntime(1123):在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) :E/AndroidRuntime(1123) :在 android.graphics.drawable.Drawable.createFromStream(Drawable.java:791) :E/AndroidRuntime(1123):在 com.c.fragments.AuthorImageFragment.loadImageFromWebOperations(AuthorImageFragment.java:77) :E/AndroidRuntime( 1123):at com.c.fragments.AuthorImageFragment.access $ 0(AuthorImageFragment.java:72) :E/AndroidRuntime(1123):at com.c.fragments.AuthorImageFragment $ 1.run(AuthorImageFragment.java:109)

,这里是从适配器代码:

private ImageView imageDetail(View v, int resId, Drawable drawable) { 
     ImageView value = (ImageView) v.findViewById(resId); 
     BitmapDrawable draw = (BitmapDrawable)drawable; 
//  value.setAdjustViewBounds(true); 
     Bitmap bmp=draw.getBitmap(); 
     // value.setLayoutParams(new android.widget.TableRow.LayoutParams((width/4), (height/4))); 
     value.setImageBitmap(bmp); 

     return value; 

    } 





Thread networkThread = new Thread() { 
       @Override 
       public void run() { 
        Author author; 
        // This is just a test i want to do :))) 
        for (int i=0;i<=1000;i++){ 
         author=new Author(); 

         try { 
         author.setAuthorImage(loadImageFromWebOperations("http://mySite/Directory/images/"+5+".jpg",i)); 
        } catch (MalformedURLException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
         list.add(author); 
        } 
        getActivity().runOnUiThread (new Runnable(){ 
         public void run() { 
          adapter= new AuthorImageAdapter(getActivity().getApplicationContext(),list); 
         } 
        }); 
      } 
     }; networkThread.start(); 

****这里是我的方法用于检索图片****

private Drawable loadImageFromWebOperations(String url,int id) throws MalformedURLException, IOException 
     { InputStream is=null; 
       try{ 
      is = (InputStream) new java.net.URL(url).getContent(); 
      LOGGER.info("Drawable -----"); 
      Drawable d = Drawable.createFromStream(is, id+".jpg"); 
      return d; 
      }catch (Exception e) { 
      // log Exception 
     LOGGER.info("Exception "+e.toString()); 
      return null; 
      } 
       finally { 
        if (is!=null){ 
        try { 
         is.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         LOGGER.info("Exception "+e.toString()); 
    // log Exception 
         return null; 
        } 
       } 
       } 
     } 

任何帮助将不胜感激.. 最好的问候:))) 现在来帮助更多我将添加我用来检索图像的代码。

回答

0

你不可能在记忆中保存许多图像。我建议你将图像下载到本地文件存储器,然后在你想要真正绘制具有图像的项目时,在适配器中,你应该启动一个Loader来按需从磁盘加载图像。对不起,没有足够的内存一次存储多个图像。

更高级的策略是使用WeakReferences将缓存插入加载过程。你从磁盘加载镜像并插入到像HashMap这样的东西中,然后看看你想要的镜像是否在第一个镜像中,如果没有,那么就像平常一样从磁盘上加载。

如果你想让体系结构真的很喜欢上面列出的WeakReference缓存,试着从磁盘加载,但是如果文件不存在于磁盘上,只有从网络加载。然后在最终点击网页之前,它变成了双层缓存内存,然后是磁盘。