How to show an image from an url in android

前端 未结 2 591
傲寒
傲寒 2020-12-06 08:25

I have looked around the internet but haven\'t been able to find a working example on how to show an image from an url.

The below code is crashing a

相关标签:
2条回答
  • 2020-12-06 09:06

    Do not try Bitmap in the code. Just use simple Drawable image and implement its method like below. you will display image 100 percent

    Drawable drw =LoadImageFromWebOperations(imageUrl);
    image.setImageDrawable(drw);
    
    return  image;
    
    private Drawable LoadImageFromWebOperations(String strPhotoUrl) 
        {
            try
            {
            InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
            }catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
            }
    
    0 讨论(0)
  • 2020-12-06 09:11

    Just use the following method to draw image from url:

    Drawable drawable_from_url(String url, String src_name) throws 
       java.net.MalformedURLException, java.io.IOException 
    {
       return Drawable.createFromStream(((java.io.InputStream)
          new java.net.URL(url).awagetContent()), src_name);
    }
    

    Just pass the string url to the method(and for src_name any string ) and it will return you a drawable object, then use setBckgroundDrawable() method of the imageview to set the background of the image.

    0 讨论(0)
提交回复
热议问题