【问题标题】:Android AsyncTask download image errorsAndroid AsyncTask 下载图片错误
【发布时间】:2010-11-10 20:53:46
【问题描述】:
   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:错误/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

解决办法是什么?

【问题讨论】:

  • 天哪,这真是一团糟。切勿致电System.gc();。永远永远。第二,handler.post(r);。天哪,这就是 asynctask 的用途。在 onpostexecute 中执行此操作。

标签: android image


【解决方案1】:

不要重新发明轮子。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多