2010-11-10 60 views
0
private static class asyncDownloadImage extends AsyncTask<Object, Integer, Integer> 
    { 
     Bitmap _image=null; 

     @Override 
     protected Integer doInBackground(Object... params) { 

      String _url=com.nag.online.utils.objectToString(params[0]); 

      byte tries=0; 

      do { 
       _image   = com.nag.online.utils.downloadPicture(_url); 

       try {Thread.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); } 

       tries++; if (tries>utils.TRIES) break; 
      } while (_image==null); 

      final Bitmap _imagex=_image; 

      Runnable r = new Runnable() { 
       public void run() { 
        ImageViewImage.setImageBitmap(_imagex); 
        ImageViewImage.setScaleType(ScaleType.CENTER); 
        ImageViewImage.refreshDrawableState(); 
       }}; 

      handler.post(r); 
      isReady=true; 

      return 0; 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 
      /*if (!_image.isRecycled()) { 
       _image.recycle(); 
       _image=null; 
       System.gc(); 
      }*/ 

      System.gc(); 
     } 
    } 

我得到:的Android的AsyncTask下載圖像誤差

11-10 13:32:07.057:ERROR/dalvikvm(4904):內存:堆大小= 8455KB,分配= 5755KB,位圖大小= 7855KB

,或者當我要把它放到我的代碼的 「onPostExecute」:

if (!_image.isRecycled()) { 
       _image.recycle(); 
       _image=null; 
       System.gc(); 
      } 

然後我得到:

11-10 13:40:55.117:電子RROR/AndroidRuntime(4981):java.lang.RuntimeException:畫布:試圖使用回收的位圖[email protected]

這是什麼解決方案?

+2

聖地獄這是一團糟。切勿調用System.gc();'。永遠永遠。其次,'handler.post(r);'。耶穌基督這就是asynctask的用途。在onpostexecute中這樣做。 – Falmarri 2010-11-10 19:56:43

回答

1

不重新發明車輪。

有人已經做到了。找到它here

+0

任何其他可用的評論? – lacas 2010-11-10 18:25:34