来自网址的Android图片视图

时间:2012-08-29 07:34:07

标签: android image url

我正在从网址下载图片,但下载完成后图片不会更改。 我在下面输入代码,有人经历过相同的代码吗?

Java档案

public class MyImgActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView imgView =(ImageView)findViewById(R.id.imageView1);
    Drawable drawable = LoadImageFromWebOperations("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");

    imgView.setImageDrawable(drawable);
 }
private Drawable LoadImageFromWebOperations(String url) {
    try
      {
       InputStream is = (InputStream) new URL(url).getContent();
       Drawable d = Drawable.createFromStream(is, "src name");
       return d;
      }catch (Exception e) {
       System.out.println("Exc="+e);
       return null;
      }
}
}

XML档案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView 
    android:id="@+id/imageView1"
    android:layout_height="match_parent" 
    android:layout_width="match_parent"></ImageView>
</LinearLayout>

Manifest档案

<uses-permission android:name="android.permission.INTERNET"/>

4 个答案:

答案 0 :(得分:17)

请使用以下代码下载并将图像显示到imageview。

public class image extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }
}

答案 1 :(得分:3)

以下代码使用以下网址,但它无法使用您的网址。问题在于您的图片大小。请尝试使用其他网址。

public class MyImgActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView imgView =(ImageView)findViewById(R.id.imageView1);
        URL url = null;
        Bitmap bmp = null;
        try {
            url = new URL("http://www.seobook.com/images/smallfish.jpg");
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (MalformedURLException e) {

        }catch (IOException e) {

        }
       imgView.setImageBitmap(bmp); 
      }

    }

答案 2 :(得分:1)

 try
  {

   URL murl = new URL(url)
  URLConnection ucon = murl.openConnection();
  InputStream is = ucon.getInputStream();
   Drawable d = Drawable.createFromStream(is, "src name");
   return d;
  }catch (Exception e) {
   System.out.println("Exc="+e);
   return null;
  }

在你的下载方法中使用这个cose,如果连接速度很慢,请使用thread to donwload and hanlder发布图片......如@Hiren所述

答案 3 :(得分:0)

尝试以下代码,

只需将您的网址更改为

即可

URL

的onCreate

new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            drawable = LoadImageFromWebOperations("http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
            handler.sendEmptyMessage(0);
        }
    }).start();

使用处理程序

下载图像后设置图像
Handler handler = new Handler(){
    @Override
    public void handleMessage(android.os.Message msg) {
        imgView.setImageDrawable(drawable);
        Log.i("System out","after set the image...");
    }
};

希望帮助你...