編輯:關於 Android 程式設計
幾乎每個應用都需要從網路抓取圖片並顯示出來。本文將實作一個這樣的例子:使用者在輸入框貼上圖片網址,點擊按鈕後,程式在子線程中下載圖片,再透過 Handler 更新到畫面的 ImageView 上。

界面包含三個控制項:一個 EditText、一個 Button 與一個 ImageView。
<EditText
android:id="@+id/picturepagh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查看" />
<ImageButton
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="200px" />
點擊按鈕時啟動子線程下載,下載完成後透過 Handler 把 Bitmap 送回主線程更新畫面:
public class MainActivity extends Activity {
private Button btn;
private EditText path;
private ImageView imgview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
path = (EditText) findViewById(R.id.picturepagh);
imgview = (ImageView) findViewById(R.id.imageView);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runa).start(); // 在子線程中下載
}
});
}
public void setView() {
String picturepath = path.getText().toString();
try {
byte[] data = ImageService.getImage(picturepath);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // 圖片工廠
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "獲取圖片錯誤", 1).show();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
updateImageView((Bitmap) msg.obj);
}
}
};
private Runnable runa = new Runnable() {
@Override
public void run() {
setView();
}
};
private void updateImageView(Bitmap bm) {
imgview.setImageBitmap(bm);
}
}
封裝一個透過 HttpURLConnection 抓取網路圖片位元組的方法:
public class ImageService {
// 取得網路圖片的資料
public static byte[] getImage(String picturepath) throws Exception {
URL url = new URL(picturepath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 基於 http 協定的連線物件
conn.setConnectTimeout(10); // 10 秒逾時
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream ins = conn.getInputStream();
return StreamTool.read(ins);
}
return null;
}
}
把輸入串流讀取為位元組陣列:
public class StreamTool {
public static byte[] read(InputStream ins) throws Exception {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = ins.read(buffer)) > -1) {
outstream.write(buffer, 0, length);
}
outstream.close();
return outstream.toByteArray();
}
}
最後別忘了在清單檔中宣告網路權限,否則程式無法連線:
<uses-permission android:name="android.permission.INTERNET" />
這個範例完整地串起了「子線程下載 → 位元組轉 Bitmap → Handler 更新 UI」的經典流程,是理解 Android 網路圖片載入機制的最佳入門實作。
Mob 短信驗證 SDK 接入實戰:從申請密鑰到服務端驗證
本文目錄 Mob 短信服務簡介;前期準備(註冊、創建應用、下載與引入 SDK);調用帶界面的驗證接口;調用無界面接口;EventHandler 回調機制;智能短信驗證;服
Android 中使用 DownloadManager 下載並安裝 APK
DownloadManager 是 Android 提供的系統下載服務,適合處理長時間的 HTTP 下載。它會在背景執行下載,並處理失敗重試、網路切換與重新開機後的續傳情
Android Studio Intent 隱式啟動實戰:發短信、撥號、打電話與開啟網頁
Intent 的隱式啟動是 Android 四大元件解耦的關鍵。只要正確宣告 action 與 data,就能把工作交給系統或其他 App 完成,不必關心對方是誰。本文用
Android 百度地圖離線地圖功能實作與常見問題整理
本文整理在 Android 中實作百度地圖離線地圖的流程,包含相關 API、下載管理、列表顯示,以及開發過程中遇到的問題。 離線地圖 API 百度地圖 SDK 提供 M