Android入门:查看服务器图片应用


一、网络图片查看器需求

 存在一个Web服务器,其中存在一个图片,在Android客户端能够访问这张图片并在Android客户端显示;

 

当点击“提交”后,则会显示指定服务器的图片;

需要注意的一点是:我们不能使用localhost表示本机,而需要使用局域网的IP地址,否则会抛Connection confused异常;

二、核心代码介绍

 在AndroidManifest.xml中加入:

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

(1)URL url = new URL("http://.....");   //将字符串转为URL类型

(2)HttpURLConnection conn = (HttpURLConnection)url.openConnection();

(3)conn.setRequestMethod("GET");     //设置请求方法,如GET POST

(4)conn.setReadTimeout(milliseconds);    //设置读超时时间

(5)int code = conn.getResponseCode();      //获得响应码,如200表示OK,404表示无资源

(6)InputStream in = conn.getInputStream();   //获得输入流

(7)Bitmap bitmap = BitmapFactory.decodeByteArray(byte[]data,int begin,int length);   // 根据byte[] 转变为位图

三、全部代码

搭建Web服务器的过程我就忽略了,此处我们使用最常用的Tomcat,版本为7.0.6;

MainActivity.java

  1. package org.xiazdong.view.image;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8.   
  9. import android.app.Activity;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.EditText;  
  17. import android.widget.ImageView;  
  18. import android.widget.Toast;  
  19.   
  20. public class MainActivity extends Activity {  
  21.     private EditText editText;  
  22.     private Button button;  
  23.     private ImageView imageView;  
  24.     private OnClickListener listener = new OnClickListener(){  
  25.   
  26.         @Override  
  27.         public void onClick(View v) {  
  28.             Bitmap bitmap = null;  
  29.             try {  
  30.                 bitmap = getImage(editText.getText().toString());  
  31.             } catch (Exception e) {  
  32.                 e.printStackTrace();  
  33.                 Toast.makeText(MainActivity.this"获取图片失败",Toast.LENGTH_SHORT).show();  
  34.             }  
  35.             if(bitmap!=null)  
  36.                 imageView.setImageBitmap(bitmap);  
  37.             else{  
  38.                 Toast.makeText(MainActivity.this"获取图片失败",Toast.LENGTH_SHORT).show();  
  39.             }  
  40.         }  
  41.   
  42.         private Bitmap getImage(String path) throws Exception {  
  43.             URL url = new URL(path);  
  44.             HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  45.             byte[]data ;  
  46.             con.setRequestMethod("GET");  
  47.             if(con.getResponseCode()==200){  
  48.                 InputStream in = con.getInputStream();  
  49.                 data = read2Byte(in);  
  50.                 return BitmapFactory.decodeByteArray(data, 0, data.length);  
  51.             }  
  52.             else return null;  
  53.         }  
  54.     };  
  55.     @Override  
  56.     public void onCreate(Bundle savedInstanceState) {  
  57.         super.onCreate(savedInstanceState);  
  58.         setContentView(R.layout.main);  
  59.         editText = (EditText)this.findViewById(R.id.imagepath);  
  60.         button = (Button)this.findViewById(R.id.button);  
  61.         imageView = (ImageView)this.findViewById(R.id.imageview);  
  62.         button.setOnClickListener(listener);  
  63.     }  
  64.     private byte[] read2Byte(InputStream in) throws IOException {  
  65.         byte[] data;  
  66.         ByteArrayOutputStream bout = new ByteArrayOutputStream();  
  67.         byte[]buf = new byte[1024];  
  68.         int len = 0;  
  69.         while((len = in.read(buf))!=-1){  
  70.             bout.write(buf, 0, len);  
  71.         }  
  72.         data = bout.toByteArray();  
  73.         return data;  
  74.     }  
  75. }  
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="图片路径"  
  11.          />  
  12.     <!-- 此处不能用localhost,一定要用ip地址 -->  
  13.     <EditText   
  14.         android:id="@+id/imagepath"  
  15.          android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="http://192.168.0.103:8080/Server/logo.png"   
  18.         />  
  19.     <Button   
  20.         android:id="@+id/button"  
  21.          android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="提交"  
  24.         />  
  25.     <ImageView   
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:id="@+id/imageview"  
  29.         />  
  30.   
  31. </LinearLayout>  

相关内容