在我们的手机应用开发中,经常要进行客户端与服务端之间的交互,我们可能会从网络上获取一张图像显示在我们的手机上,这样我们可以使用HTTP协议进行操作。这一讲我们将学习如何从服务端获取图片到手机客户端
案例:点击按钮从网络上获取ImageView图像显示在本地
程序实现:
案例:点击按钮从网络上获取ImageView图像显示在本地
程序实现:
1. 布局文件
<?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/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载网络图片"/> </LinearLayout>
2. 程序主要代码
1)HttpUtils.java 作为辅助类[注意编程规范]: 如果方法A里面含有了 try catch模块来捕获异常,在方法B中调用A的过程中也需要try catch 一次异常。则这种情况下一般要把A的异常抛出,让方法B去捕获这个异常。
public class HttpUtils { private final static String URL_PATH = "http://www.android.com/images/about/about-nexus-family.png"; //访问网络图片的路径 public HttpUtils() { // TODO Auto-generated constructor stub } /** * 从网络中获取图片信息,以流的形式返回 * @return * @throws IOException */ public static InputStream getImageViewInputStream() throws IOException{ InputStream inputStream = null; URL url = new URL(URL_PATH); if(url != null){ HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //设置连接超时的时间 httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); //设置响应的代码 int response_code = httpURLConnection.getResponseCode(); if(response_code == 200){ inputStream = httpURLConnection.getInputStream(); } } return inputStream; } public static byte[] getImageViewArray(){ byte[] data = null; InputStream inputStream = null; //不需要关闭输出流,直接 写入到内存中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { URL url = new URL("URL_PATH"); if(url != null){ HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //设置连接超时的时间 httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); //设置响应的代码 int response_code = httpURLConnection.getResponseCode(); int len = 0; byte[] b_data = new byte[1024]; if(response_code == 200){ inputStream = httpURLConnection.getInputStream(); while((len = inputStream.read(b_data)) != -1) { outputStream.write(b_data, 0, len); } data = outputStream.toByteArray(); } } } catch (Exception e) { // TODO: handle exception }finally{ if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return data; } }
2) 主程序
public class ImageViewDemo extends Activity { private Button button; private ImageView imageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponent(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(){ public void run() { try { InputStream inputStream = HttpUtils.getImageViewInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); imageView.setImageBitmap(bitmap); //需要一个Bitmap对象,可以从 BitmapFactory工厂中获得 } catch (IOException e) { // TODO: handle exception } }; }.start(); } }); } private void initComponent(){ button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageView); } }
访问网络的主程序最好另起一个线程而不是写在UI主线程中。