How to display an image from the internet in android?

前端 未结 4 1270
被撕碎了的回忆
被撕碎了的回忆 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:38

    First hit the image link,then you will get the image as byte array.Now just decode the byte array to bitmap image.Lets take a look:

    package Image.Read.a;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    import android.graphics.BitmapFactory;
    
    public class Connecetion1
    {
     public void setNetwork()
     {
        try
        {
    
            URL url = new URL("http://3.bp.blogspot.com/_9UYLMDqrnnE/S4UgSrTt8LI/AAAAAAAADxI/drlWsmQ8HW0/s400/sachin_tendulkar_double_century.jpg");
    
            URLConnection connection=url.openConnection();
    
            HttpURLConnection HCon=(HttpURLConnection)connection;
    
            int ResCode=HCon.getResponseCode();
    
            System.out.println("Responce Code is = "+ResCode);
    
    
            if(ResCode==HttpURLConnection.HTTP_OK)
            {
    
            InputStream ins=((URLConnection)HCon).getInputStream();   
    
                  Data.StoreImg=BitmapFactory.decodeStream(ins);
    
    
            }
    
        }
        catch (MalformedURLException e)
            {
    
            e.printStackTrace();
        } catch (IOException e)
            {
    
              e.printStackTrace();
             }
    
     }
    

    }

    You can get the full tutorial from http://www.androidcookers.blogspot.com/2011/06/retrieve-image-from-internet.html

提交回复
热议问题