android 之使用多线程中的AsyncTask实现下载网络图片资源
生活随笔
收集整理的這篇文章主要介紹了
android 之使用多线程中的AsyncTask实现下载网络图片资源
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前臺顯示:
<?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"><ImageViewandroid:id="@+id/imageView_img"android:src="@mipmap/ic_launcher"android:layout_height="200dp"android:layout_width="200dp"android:layout_gravity="center_horizontal"/><Buttonandroid: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;@Overrideprotected 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>{@Overrideprotected void onPreExecute(){progressDialog.setMessage("正在下載網絡圖片,請稍等。。。");progressDialog.show();}/*** 網絡下載操作* @param params* @return*/@Overrideprotected 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;}@Overrideprotected void onProgressUpdate(Integer...values){switch (values[0]){case 0:progressDialog.setMax(values[1]);break;case 1:progressDialog.incrementProgressBy(values[1]);break;}}@Overrideprotected 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"/>這句權限代碼才可以實現要實現的功能。總結
以上是生活随笔為你收集整理的android 之使用多线程中的AsyncTask实现下载网络图片资源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 之多线程详解
- 下一篇: 15年三菱劲炫手动1.6排量简介?