Loading remote images

前端 未结 5 1439
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 09:37

In Android, what is the simplest approach to the following:

  1. Load an image from a remote server.
  2. Display it in an ImageView.
5条回答
  •  我在风中等你
    2020-11-29 10:17

    The easiest way so far is build a simple image retriver:

    public Bitmap getRemoteImage(final URL aURL) {
        try {
            final URLConnection conn = aURL.openConnection();
            conn.connect();
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            final Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            return bm;
        } catch (IOException e) {}
        return null;
    }
    

    Then, you just have to supply a URL to the method and it will returns a Bitmap. Then, you will just have to use the setImageBitmap method from ImageView to show the image.

提交回复
热议问题