开发者

Android AsyncTask download image errors

开发者 https://www.devze.com 2023-01-24 14:38 出处:网络
private static class asyncDownloadImage extends AsyncTask<Object, Integer, Integer> { Bitmap _image=null;
   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();
        }
    }

i get:

11-10 13:32:07.057: ERROR/dalvikvm(4904): Out of memory: Heap Size=8455KB, Allocated=5755KB, Bitmap Size=7855KB

or when i put this into my code on "onPostExecute":

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

then i get:

11-10 13:40:55.117: ERROR/AndroidRuntime(4981): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@44cadc98

what is the solution for this?


Not reinventing the wheel.

Someone already did it. Find it here.

0

精彩评论

暂无评论...
验证码 换一张
取 消