2010-11-10 51 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