Skip to content Skip to sidebar Skip to footer

How To Display An Image From The Internet In Android?

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

Solution 1:

You can use the method setImageDrawable

ImageViewiv=newImageView;

URLurl=newURL(address);
InputStreamcontent= (InputStream)url.getContent();
Drawabled= 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);

Solution 2:

I think you can use the setImageUri method. The URI can be built using Uri.parse.

Solution 3:

first u need to hit image url and store the server Data as byte array, then u need to convert these byte data into Bitmap image.. Here is the code

                String myfeed="http://174.136.1.35/dev/atmsearch/visa.jpg";

                try{

                    URL url=new URL(myfeed);
                    URLConnection connection=url.openConnection();
                    connection.setDoOutput(true);
                    connection.setDoOutput(true);
                    connection.setRequestProperty("METHOD", "POST");
                    connection.setRequestProperty("Content-Type","application/x-www-from-urlencoded");

                    HttpURLConnection httpConnection=(HttpURLConnection)connection;

                    int responsecode=httpConnection.getResponseCode();

                    if(responsecode==HttpURLConnection.HTTP_OK){
                        InputStream in=((URLConnection)httpConnection).getInputStream();
                        intlen=0;
                        Bitmap b=BitmapFactory.decodeStream(in);



                        System.out.println(b.toString());


                        byte[] data1=newbyte[1024];

                        while(-1!=(len=in.read(data1))){
                            System.out.println("--input stream--");
                            datafromserver.append(new String(data1,0,len));

                        }
                        //System.out.println(datafromserver);
                    }

                }catch(IOException e){
                    System.out.println("Error...."+e);
                    //Toast.makeText(context, text, duration)

                }

// Now set the bitmap image in image view imageview.setImageBitmap(b);

Solution 4:

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;

publicclassConnecetion1
{
 publicvoidsetNetwork()
 {
    try
    {

        URLurl=newURL("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTEWCekaIK7BXEXG6QNELn_4JRta9ID-WF6nUYCEtAYc1gvG_yNmctiabLvvtG2uw_q-GvFf5NBIH4GLw5tscAbZ6K4C4KOcVCEX4ZCLTp_KJy2kZEXHVLl47IkiiHxyg8TgXywrzyiQQ/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

Post a Comment for "How To Display An Image From The Internet In Android?"