3
protected Bitmap doInBackground(String...params) {
        // TODO Auto-generated method stub
         try {

              /* Open a new URL and get the InputStream to load data from it. */

              URL aURL = new URL(params[0]);
              URLConnection conn = aURL.openConnection();
              conn.connect();
              InputStream is = conn.getInputStream();

              /* Buffered is always good for a performance plus. */
              BufferedInputStream bis = new BufferedInputStream(is);

              /* Decode url-data to a bitmap. */
              Bitmap bm = BitmapFactory.decodeStream(bis);
              b1=bm;

              bis.close();
              is.close();

          } catch (IOException e) 
          {

              Log.e("DEBUGTAG", "Remote Image Exception", e);

          }

       return null;
   }

// TODO Auto-generated method stub
        super.onPostExecute(result);

        i1.setImageBitmap(b1);
        Animation rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
     i1.startAnimation(rotation);
    //System.out.println("imageview is working");
        try {
            System.out.println("thread going to sleep");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        i1.setImageBitmap(b1);
        }

我正在通过位图在图像视图中显示来自互联网的图像现在我通过更新 url 获得了另一个图像这个位图但是我无法显示这个图像只有前一个图像显示,如果我在一个仅循环显示最后一张图片?任何建议

4

1 回答 1

1

从您的 OnCreate 或您想从网络下载代码的任何地方调用它

new Thread(new Runnable() {
                public void run() {
                    Drawable drawable = createDrawableFromURL(_mDrawingURL);

                    Message msgURL = new Message();
                    msgURL.arg2 = URL_CONSTANT;
                    msgURL.obj = drawable;
                    _handler_url.sendMessage(msgURL);   
                }
            }).start();
        } catch (JSONException e) {
            e.printStackTrace();
        } 
    }

此方法将用于从 Internet 下载图像:

private Drawable createDrawableFromURL(String urlString) {
        Drawable image = null;
        try
        {
            InputStream inputStream = new URL(urlString).openStream();
            image = Drawable.createFromStream(inputStream, null);
            inputStream.close();
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            image = null;
        } catch (IOException e) {
            e.printStackTrace();
            image = null;
        }
        return image;
    }

下载图片后,imageview 会更新图片:

private Handler _handler_url = new Handler(){
        public void dispatchMessage(Message msg) {
            switch (msg.arg2) {
            case URL_CONSTANT:
                Drawable drawable = (Drawable) msg.obj;
                if(drawable!=null)
                    _mImageView.setImageDrawable(drawable);

                break;
            default:
                break;
            }
        };
    };
于 2012-12-20T09:09:44.307 回答