How to show an image from an url in android

好久不见. 提交于 2019-11-27 23:21:35
Dinesh Sharma

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.

micky

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;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!