在寫 Android 之前,先用純 Java 跑一次,確認邏輯沒問題:
public static void main(String[] args) throws Exception {
String path = "https://img.linuxidc.com/img/Android.png";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
File f = new File("pic.jpg");
FileOutputStream fos = new FileOutputStream(f);
fos.write(data);
fos.close();
}
資源:
<string name="btn_text">顯示網絡圖片</string> <string name="error">下載圖片失敗!!</string>
版面:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_text"
android:id="@+id/showBtn" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
ImageService 負責把遠端檔案讀成位元組陣列:
package cn.class3g.service;
public class ImageService {
public static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
// get // post
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();// 圖片的二進位資料
outStream.close();
inStream.close();
return data;
}
}
Activity 端把位元組解成 Bitmap 再塞進 ImageView:
public void onClick(View v) {
String path = "https://img.linuxidc.com/img/Android.png";
try {
byte[] data = ImageService.getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imgView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.e("TAG", e.toString());
Toast.makeText(this, R.string.error, 1).show();
}
}
版面與上面相同,程式只要把 ImageService.getImage() 換成 HtmlService.getHtml():
public class HtmlService {
/**
* 取得指定路徑的 html 程式碼
* @param path 網頁路徑
* @return
* @throws Exception
*/
public static String getHtml(String path) throws Exception {
URL url = new URL(path);
// get // post
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();// 網頁的二進位資料
outStream.close();
inStream.close();
return new String(data, "gb2312");
}
}
public void onClick(View v) {
String path = pathText.getText().toString();
try {
String htmlcode = HtmlService.getHtml(path);
resultView.setText(htmlcode);
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show();
}
}
三個範例共用同一套骨架:開連線、設定逾時與方法、迴圈讀串流、彙整成位元組,差別只在最後是把資料存成檔案、解成圖片,還是依指定編碼轉成字串。實作時記得加上 INTERNET 權限,並把網路工作放到背景執行緒。