Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

簡易實作 Android 讀取網路圖片並顯示於本地

編輯:關於 Android 程式設計

今天在网上看到一段關於讀取網路檔案的教學影片,覺得不錯,特別整理出來與大家分享。

思路

整體思路其實很單純:輸入一個網址、點擊按鈕,就把從網路抓取的圖片顯示到 ImageView 上。如此一來,核心便是網路操作——說穿了就是讀取網路串流檔案。

程式碼展示

先看主界面的版面檔:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_website" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="please type the url " /> <Button android:id="@+id/btn_get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check" /> <ImageView android:id="@+id/iv_picture" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>

接著是主界面的邏輯程式碼:

 package com.example.getphotobyxml; 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; import com.example.service.ImageService; public class MainActivity extends Activity { private EditText mEt_url; private ImageView mIv_picture; private Button mBtn_get; /** * 初始化相關的需要使用到的ID */ public void init() { mEt_url = (EditText) findViewById(R.id.et_website); mIv_picture = (ImageView) findViewById(R.id.iv_picture); mBtn_get = (Button) findViewById(R.id.btn_get); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //記得要調用哦 init(); mBtn_get.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String website = mEt_url.getText().toString(); if (website == null || website.equals("")) { Toast.makeText(MainActivity.this, "請輸入正確的網址哦!", Toast.LENGTH_LONG).show(); return; } byte[] bytes; try { bytes = ImageService.getImage(website); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); mIv_picture.setImageBitmap(bitmap); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } /** * 從網絡以XML的方式獲得一張圖片,並顯示到一個ImageView上 * 按鈕事件可以直接不注冊onClickListener,而使用這個方法 * @param view */ public void getPicture(View view) { String website = mEt_url.getText().toString(); if (website == null || website.equals("")) { Toast.makeText(this, "請輸入正確的網址哦!", Toast.LENGTH_LONG).show(); return; } byte[] bytes; try { bytes = ImageService.getImage(website); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); mIv_picture.setImageBitmap(bitmap); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

service 與 tools 助手類:

 package com.example.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import com.example.utils.StreamTool; /** *圖片服務的業務類 */ public class ImageService { public static byte[] getImage(String website) throws Exception { URL url = new URL(website); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200){ InputStream inputStream = conn.getInputStream(); byte[] bytes = StreamTool.read(inputStream); return bytes; } return "讀取網絡數據失敗".getBytes(); } }
 package com.example.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; /** *專門用於將輸入流轉換成一個字節數組的utils類 */ public class StreamTool { public static byte[] read(InputStream inputStream) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while((len = inputStream.read(buf))!=-1){ baos.write(buf, 0 ,len); } baos.close(); return buf; } }

總結

這裡的程式碼相當簡單,我貼出來主要是想展示分層的思想與重構的藝術。你會看到,我們為特定的工作建立了專門的類,不同層次的類處理不同業務,這有助於以物件導向的方式編程,帶來更清晰的邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved