Loading remote images

前端 未结 5 1435
被撕碎了的回忆
被撕碎了的回忆 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:11

    Here's a method that I actually used in an application and I know it works:

    try {
        URL thumb_u = new URL("http://www.example.com/image.jpg");
        Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
        myImageView.setImageDrawable(thumb_d);
    }
    catch (Exception e) {
        // handle it
    }
    

    I have no idea what the second parameter to Drawable.createFromStream is, but passing "src" seems to work. If anyone knows, please shed some light, as the docs don't really say anything about it.

提交回复
热议问题