Android入門:查看伺服器圖片應用

來源:互聯網
上載者:User


一、網狀圖片查看器需求

 

存在一個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[] 轉變為位元影像

(8)imageView.setImageBitmap(Bitmap bitmap);

三、全部代碼

搭建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(){@Overridepublic 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.