在Android中實(shí)現(xiàn)異步任務(wù)機(jī)制有兩種方式,Handler和AsyncTask。這篇文章給大家介紹Android使用AsyncTask下載圖片并顯示進(jìn)度條功能。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到岑溪網(wǎng)站設(shè)計(jì)與岑溪網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋岑溪地區(qū)。AsyncTask下載圖片并顯示下載進(jìn)度,異步類AsyncTask配合進(jìn)度條,簡(jiǎn)練!
public class AsyncTaskActivity2 extends Activity { private Button btnDown;//圖片框 private ImageView ivImage;//圖片URL private static String image_path = "/file/tupian/20230213/404.html"; //進(jìn)度對(duì)話框 private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.asynctask_activity); btnDown = (Button) findViewById(R.id.btnDown); ivImage = (ImageView) findViewById(R.id.ivSinaImage); //新建一個(gè)對(duì)話框,不顯示 dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMessage("正在下載,請(qǐng)稍后..."); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//帶有水平滾動(dòng)條 dialog.setCancelable(false);//不能 //單擊按鈕后執(zhí)行AsyncTask,并傳入圖片URL btnDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 執(zhí)行異步任務(wù) new MyTask().execute(image_path);//啟動(dòng)AsyncTask,參數(shù)類型為異步類的第一個(gè)參數(shù)的類型 } }); } //創(chuàng)建一個(gè)內(nèi)部類,用來(lái)下載圖片 public class MyTask extends AsyncTask{ @Override protected void onPreExecute() {//啟動(dòng)異步類后首先運(yùn)行,可以設(shè)置 super.onPreExecute(); dialog.show();//顯示對(duì)話框 } //第二步,一般進(jìn)行復(fù)雜處理 @Override protected Bitmap doInBackground(String... params) {//最重要,復(fù)雜處理,后臺(tái),接收execute傳來(lái)的參數(shù) Bitmap bitmap = null; //二進(jìn)制圖片 //字節(jié)數(shù)組輸出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = null;//字節(jié)輸入流 try { HttpClient httpClient = new DefaultHttpClient();//創(chuàng)建一個(gè)Http客戶端 HttpGet httpGet = new HttpGet(params[0]);//發(fā)送請(qǐng)求 HttpResponse httpResponse = httpClient.execute(httpGet);//返回 if (httpResponse.getStatusLine().getStatusCode() == 200) {//成功 inputStream = httpResponse.getEntity().getContent();//得到返回內(nèi)容作為輸入流 long file_length = httpResponse.getEntity().getContentLength();//文件長(zhǎng)度 int len = 0; byte[] data = new byte[1024];//每次讀取的字節(jié) int total_length = 0; // 以字節(jié)的方式讀取圖片數(shù)據(jù) while ((len = inputStream.read(data)) != -1) { total_length += len; // 計(jì)算進(jìn)度 int values = (int) ((total_length / (float) file_length) * 100); // 發(fā)布進(jìn)度信息 publishProgress(values);//發(fā)布進(jìn)度,AsyncTask第二個(gè)參數(shù)類型,觸發(fā)onProgressUpdate更新進(jìn)度條 outputStream.write(data, 0, len);//寫入輸出流 } byte[] result=outputStream.toByteArray();//將字節(jié)數(shù)組輸出流轉(zhuǎn)換為字節(jié)數(shù)組 //生成二進(jìn)制圖片 bitmap=BitmapFactory.decodeByteArray(result, 0, result.length); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e2) { } } return bitmap; } //當(dāng)有 publishProgress(value)發(fā)來(lái)的數(shù)據(jù)時(shí)觸發(fā),更新UI @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); // 設(shè)置進(jìn)度對(duì)話框的進(jìn)度值 dialog.setProgress(values[0]);//更新進(jìn)度條,運(yùn)行在UI } //第四步 @Override protected void onPostExecute(Bitmap result) {//下載結(jié)束后,隱藏對(duì)話框 super.onPostExecute(result); dialog.dismiss(); ivImage.setImageBitmap(result); //更新UI,顯示圖片,運(yùn)行在UI } } }