How to display an image from the internet in android?

前端 未结 4 1254
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 12:04

How can I display image in an ImageView in android from a URL (from the internet)?

4条回答
  •  囚心锁ツ
    2020-12-09 12:27

    You can use the method setImageDrawable

    ImageView iv = new ImageView;
    
    URL url = new URL(address);
    InputStream content = (InputStream)url.getContent();
    Drawable d = Drawable.createFromStream(content , "src"); 
    iv.setImageDrawable(d)
    

    [2014-12-16] Edit: Using Picasso, makes your life much simplier

    String url = "http://i.imgur.com/bIRGzVO.jpg";
    ImageView iv = new ImageView;
    
    Picasso.with(context).load(url).into(iv);
    //Picasso.with(context).load(url).centerCrop().fit().into(iv);
    

提交回复
热议问题