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緩存,試着從磁盤加載,但是如果文件不存在於磁盤上,只有從網絡加載。然後在最終點擊網頁之前,它變成了雙層緩存內存,然後是磁盤。

相關問題