2012-06-11 21 views
0

我正在構建一個新聞紙應用程序,因爲我需要在gallary視圖中顯示新聞紙張圖像(epaper)...我需要下載大約100張圖像。爲此,我使用asyncTask和每個下載圖像創建新的AsyncTask對象,並且當我嘗試下載圖像並將其設置爲gallary時,我在中間出現錯誤「VM不會讓我們分配......字節」並崩潰應用程序。虛擬機不會讓我們分配...下載圖片時的字節數

new AsyncTask<String, Void, Bitmap>() { 

       @Override 
       protected Bitmap doInBackground(String... params) { 
        HttpGet httpRequest; 
        try { 
         httpRequest = new HttpGet(new URL(params[0]).toURI()); 
         HttpClient httpClient = new DefaultHttpClient(); 
         HttpResponse response = (HttpResponse) httpClient.execute(httpRequest); 
         HttpEntity entity = response.getEntity(); 
         BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
         InputStream is = bufHttpEntity.getContent(); 
         return BitmapFactory.decodeStream(is); 

        } catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } catch (URISyntaxException e) { 
         e.printStackTrace(); 
        } catch (ClientProtocolException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        }/* catch (Error e) { 
         e.printStackTrace(); 
        }*/ 
        return null; 
       } 

       @Override 
       protected void onPostExecute(Bitmap result) { 
        if(result != null) { 
          img.setImageBitmap(ePaperInfo.getImgJpg1()); 
         notifyDataSetChanged(); 
        } 
       } 


      } 

顯示錯誤的logcat: enter image description here

請幫我

在此先感謝。

回答

1

您應該定義不要在內存中同時保留100個位圖。您只需下載所需的位圖,然後在下載新的位圖之前調用recycle()。

看一看這個的exaple做的首選方式,你想要什麼:ImageDownloader

+0

感謝您的回放,但我需要存儲這些所有圖像,因爲我需要在「galleryview」中顯示所有圖像。 –

+0

好吧,那麼你應該嘗試實現你的適配器,像我上面發佈的ImageDownloader示例:) – Zelleriation

0

的圖像加載另一種選擇,我用的是Prime,我用它在我所有的項目和它相當簡單和高效。