一、網絡圖片查看器需求
存在一個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
- package org.xiazdong.view.image;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- private EditText editText;
- private Button button;
- private ImageView imageView;
- private OnClickListener listener = new OnClickListener(){
-
- @Override
- public void onClick(View v) {
- Bitmap bitmap = null;
- try {
- bitmap = getImage(editText.getText().toString());
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "獲取圖片失敗",Toast.LENGTH_SHORT).show();
- }
- if(bitmap!=null)
- imageView.setImageBitmap(bitmap);
- else{
- Toast.makeText(MainActivity.this, "獲取圖片失敗",Toast.LENGTH_SHORT).show();
- }
- }
-
- private Bitmap getImage(String path) throws Exception {
- URL url = new URL(path);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- byte[]data ;
- con.setRequestMethod("GET");
- if(con.getResponseCode()==200){
- InputStream in = con.getInputStream();
- data = read2Byte(in);
- return BitmapFactory.decodeByteArray(data, 0, data.length);
- }
- else return null;
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- editText = (EditText)this.findViewById(R.id.imagepath);
- button = (Button)this.findViewById(R.id.button);
- imageView = (ImageView)this.findViewById(R.id.imageview);
- button.setOnClickListener(listener);
- }
- private byte[] read2Byte(InputStream in) throws IOException {
- byte[] data;
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- byte[]buf = new byte[1024];
- int len = 0;
- while((len = in.read(buf))!=-1){
- bout.write(buf, 0, len);
- }
- data = bout.toByteArray();
- return data;
- }
- }
main.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" >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="圖片路徑"
- />
- <!-- 此處不能用localhost,一定要用ip地址 -->
- <EditText
- android:id="@+id/imagepath"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="http://192.168.0.103:8080/Server/logo.png"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="提交"
- />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageview"
- />
-
- </LinearLayout>