android 之使用多執行緒中的AsyncTask實現下載網路圖片資源
阿新 • • 發佈:2019-02-02
前臺顯示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView_img" android:src="@mipmap/ic_launcher" android:layout_height="200dp" android:layout_width="200dp" android:layout_gravity="center_horizontal" /> <Button android:onClick="click" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="下載" android:layout_gravity="center_horizontal" /> </LinearLayout>
後臺邏輯:
注意: 需要實現此功能,必須在功能清單檔案下新增package com.example.lenovo.async_imagedownload; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { private ImageView imageView_img; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.imageView_img = (ImageView) this.findViewById(R.id.imageView_img); } public void click(View view){ this.progressDialog = new ProgressDialog(this); this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); String path = "https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=61e019ca7dec54e755e1124cd851f035/7a899e510fb30f24027878f7ce95d143ac4b0367.jpg "; new myAsyncTask().execute(path); } private final class myAsyncTask extends AsyncTask<String,Integer,Bitmap>{ @Override protected void onPreExecute(){ progressDialog.setMessage("正在下載網路圖片,請稍等。。。"); progressDialog.show(); } /** * 網路下載操作 * @param params * @return */ @Override protected Bitmap doInBackground(String... params) { try{ //1.根據網址得到統一資源定位符物件 URL url = new URL(params[0]); //2.開啟連線 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); //3.設定相關引數 httpURLConnection.setConnectTimeout(3000);//三秒後如果連線不上, //向伺服器傳送請求並得到伺服器返回的狀態碼:200表示 ok //4.得到輸入流物件 InputStream inputStream = httpURLConnection.getInputStream(); //5.得到網路圖片的總大小 int size = httpURLConnection.getContentLength(); publishProgress(0,size);//0:代表報告的是檔案的總大小 ByteArrayOutputStream byteArrayOuputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = inputStream.read(buffer))!=-1){ byteArrayOuputStream.write(buffer,0,len); publishProgress(1,len);//1.代表報告下載的進度 } byte[] data = byteArrayOuputStream.toByteArray(); //將位元組陣列還原成原始圖片物件 Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length); return bitmap; }catch(Exception e){ e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Integer...values){ switch (values[0]){ case 0: progressDialog.setMax(values[1]); break; case 1: progressDialog.incrementProgressBy(values[1]); break; } } @Override protected void onPostExecute(Bitmap bitmap) { progressDialog.dismiss(); if (bitmap == null) { Toast.makeText(MainActivity.this, "網路圖片下載失敗",Toast.LENGTH_LONG).show(); return; } //將下載好的bitmap影象顯示到imageView控制元件上 imageView_img.setImageBitmap(bitmap); } } }
<uses-permission android:name="android.permission.INTERNET"/>這句許可權程式碼才可以實現要實現的功能。