Android AsyncTask下载图片错误

时间:2010-11-10 12:42:22

标签: android image

   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();
        }
    }

我得到:

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:ERROR / AndroidRuntime(4981):java.lang.RuntimeException:Canvas:尝试使用循环位图android.graphics.Bitmap@44cadc98

这是什么解决方案?

1 个答案:

答案 0 :(得分:1)

不重新发明轮子。

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