本主要介绍在android如何获取网络图片的方法,其源代码如下:

 

 
  
  1. package com.demo.lc;  
  2.  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7.  
  8. import java.net.URL;  
  9.  
  10. import android.app.Activity;  
  11.  
  12. import android.graphics.BitmapFactory;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.ImageView;  
  19. import android.widget.Toast;  
  20.  
  21. public class ImageViewActivity extends Activity  implements OnClickListener{  
  22.     /**网络路径输入框*/ 
  23.     private EditText mEt_url = null;  
  24.     /**读取网络图片*/ 
  25.     private Button mBtn_read = null;  
  26.     /**显示网络图片*/ 
  27.     private ImageView mIv_show = null;  
  28.     /**路径Url*/ 
  29.     private URL mUrl = null;  
  30.     /**http连接*/ 
  31.     private HttpURLConnection mConn = null;  
  32.     /**存储图片的二进制数组*/ 
  33.     private byte[] mPic = null;  
  34.     @Override 
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         super.setContentView(R.layout.main);  
  38.         //初始化组件  
  39.         initWidget();  
  40.         this.mBtn_read.setOnClickListener(this);  
  41.           
  42.     }  
  43.       
  44.     private void initWidget() {  
  45.         //实例化EditText  
  46.         mEt_url = (EditText) super.findViewById(R.id.et_urlpath);  
  47.         //实例化Button  
  48.         mBtn_read = (Button) super.findViewById(R.id.btn_display);  
  49.         //实例化ImageView  
  50.         mIv_show = (ImageView) super.findViewById(R.id.iv_show);  
  51.     }  
  52.     @Override 
  53.     public void onClick(View v) {  
  54.         InputStream mIs = null;  
  55.         try {  
  56.             //实例化mUrl  
  57.             mUrl = new URL(mEt_url.getText().toString());  
  58.             //建立Http网络连接  
  59.             mConn = (HttpURLConnection) mUrl.openConnection();  
  60.             //设置请求方式为GET  
  61.             mConn.setRequestMethod("GET");  
  62.             //设置超时时间为5秒  
  63.             mConn.setConnectTimeout(5000);  
  64.             //获取图片的输入  
  65.             mIs = mUrl.openStream();  
  66.             //获取图片的二进制数组  
  67.             mPic = getByteArray(mIs);  
  68.             System.out.println(mPic.length);  
  69.             mIs.close();  
  70.         } catch (Exception e) {  
  71.             //当连接超时的时候会弹出Toast  
  72.             Toast.makeText(this,R.string.error, 100).show();  
  73.             e.printStackTrace();  
  74.         }  
  75.         mIv_show.setImageBitmap(BitmapFactory.decodeByteArray(mPic, 0, mPic.length));  
  76.     }  
  77.       
  78.     private byte[] getByteArray(InputStream is)  {  
  79.         //用于向内存中写字节的字节数组输出流  
  80.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  81.         //字节数组缓冲区  
  82.         byte[] temp = new byte[1024];  
  83.         //读取字节的长度  
  84.         int len = 0;  
  85.         try {  
  86.             //以二进制的形式读入  
  87.             while((len=is.read(temp))!=-1){  
  88.                 bos.write(temp, 0, len);  
  89.             }  
  90.             is.close();  
  91.         } catch (IOException e) {  
  92.               
  93.             e.printStackTrace();  
  94.         }  
  95.         //转化为字节数组  
  96.         return bos.toByteArray();  
  97.     }  

     效果如下: