下載操作相關代碼:
package liudeli.async; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity3 extends Activity { // 圖片地址 private final String PATH = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000" + "&sec=1544714792699&di=3c2de372608ed6323f583f1c1b445e51&imgtype=0&src=http%3A%2F%2Fp" + "2.qhimgs4.com%2Ft0105d27180a686e91f.jpg"; // 成功標識 private final int SUCCESS = 200; // 失敗標識 private final int ERROR = 404; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); imageView = findViewById(R.id.iv_image); } /** * 定義Handler,用於接收子線程發過來的信息 */ private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SUCCESS: imageView.setImageBitmap((Bitmap) msg.obj); break; case ERROR: Toast.makeText(MainActivity3.this, "下載失敗,請檢查原因", Toast.LENGTH_LONG).show(); break; } if (null != progressDialog) progressDialog.dismiss(); } }; private ProgressDialog progressDialog = null; /** * 獲取圖片 按鈕 * @param view */ public void getImage(View view) { progressDialog = new ProgressDialog(MainActivity3.this); progressDialog.setMessage("正在下載..."); progressDialog.show(); // 開啟子線程 下載圖片 /** * 執行此方法才去啟動線程下載 */ new Thread(new DownloadImage()).start(); } class DownloadImage implements Runnable { /** * 發送Handler */ public void showUiImage(int responseCode, Bitmap bitmap) { Message message = mHandler.obtainMessage(responseCode); // 拿系統消息池的消息, 不要 new Message(); message.obj = bitmap; mHandler.sendMessageDelayed(message, 2000); } @Override public void run() { try { // 封裝成網絡地址 URL url = new URL(PATH); // 打開一個連接 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // 設置連接時長 httpURLConnection.setConnectTimeout(5000); // 設置請求方式 httpURLConnection.setRequestMethod("GET"); /** * 注意:⚠️ 不要膚淺的任務 打開連接對象 設置連接時長 設置請求方式 就向服務器發送Http請求了 * 是要執行httpURLConnection.getResponseCode()才會向服務器發送Http請求 */ if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 得到服務器返回過來的流對象 InputStream inputStream = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); showUiImage(SUCCESS, bitmap); } else { showUiImage(ERROR, null); } } catch (Exception e) { e.printStackTrace(); showUiImage(ERROR, null); } } } }
Layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bt_get_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取圖片" android:onClick="getImage" /> <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/bt_get_image" /> </RelativeLayout>
AndroidManifest.xml 配置網絡權限:
<uses-permission android:name="android.permission.INTERNET" />