开发者

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)?


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);


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


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();
                        int len=0;
                        Bitmap b=BitmapFactory.decodeStream(in);



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


                        byte[] data1=new byte[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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜