編輯:關於 Android 程式設計
本文示範如何透過 HttpURLConnection 從網路下載圖片,並以 ImageView 顯示。流程為:輸入圖片網址、下載位元資料、解碼為 Bitmap 後呈現。
包含一個輸入網址的 EditText、觸發下載的 Button 與顯示圖片的 ImageView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" 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="@string/urlpath" /> <EditText android:id="@+id/urlpath" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> <ImageView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.example.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }
ImageService 建立連線並回傳圖片的位元組陣列:
package com.example.image; 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 path) throws Throwable { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream(); return StreamTool.readInputStream(inStream); } }
點擊按鈕後下載圖片,以 BitmapFactory 解碼位元組並設定到 ImageView:
package com.example.image; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class ImageActivity extends Activity { private static final String TAG = "ImageActivity"; private EditText pathText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); pathText = (EditText) this.findViewById(R.id.urlpath); imageView = (ImageView) this.findViewById(R.id.show); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String path = pathText.getText().toString(); try { byte[] data = ImageService.getImage(path); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bitmap); } catch (Throwable e) { Toast.makeText(ImageActivity.this, R.string.error, Toast.LENGTH_LONG).show(); Log.e(TAG, e.toString()); } } }); } }
別忘了加入網路權限:
<uses-permission android:name="android.permission.INTERNET"/>
RecyclerView 下拉刷新與加載更多:一次講透封裝思路與踩坑記錄
「下拉刷新 + 上滑加載更多」幾乎是資訊類應用的標配交互。聽起來簡單,真正動手時卻要同時應對 RecyclerView、SwipeRefreshLayout、滑動監聽、
RecyclerView 使用詳解(一):布局管理器、分割線與點擊事件
概述 官方對 RecyclerView 的定義是: RecyclerView is a more advanced and flexible version of Li
Universal-Image-Loader 學習系列:磁碟緩存策略源碼剖析
本文聚焦 Universal-Image-Loader(UIL)的磁碟(硬盤)緩存策略。其中,LimitedAgeDiscCache 用於設定檔案存活的最長時間,一旦超過
AlertDialog 六種對話框用法一次講透
AlertDialog 生成的對話框可劃分為四個區域:圖標區、標題區、內容區、按鈕區。結構如下圖: 掌握它的關鍵,是先理解 Builder 的職責分工,再記住「內容區」