需求:使用AsyncTask下载网络图片,并更新进度条。
1、activity_main布局文件(Button和ImageView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用异步任务下载网络图片"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2、MainActivity代码实现
package com.example.zhaoyc.asynctaskdemo2;
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.Button;
import android.widget.ImageView;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView imageView;
private String img_path = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
dialog = new ProgressDialog(this);
dialog.setTitle("提示信息");
dialog.setMessage("正在下载,请稍候...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//采用横向进度条
dialog.setCancelable(false); //直到下载完自动消失
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute(img_path);
}
});
}
public class MyTask extends AsyncTask<String,Integer,Bitmap>{
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
@Override
protected Bitmap doInBackground(String... params) {
//完成对图片的下载
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200){
inputStream = httpResponse.getEntity().getContent();
//先要获得文件的总长度
long file_length = httpResponse.getEntity().getContentLength();
int len = 0;
byte[]data = new byte[1024];
int total_length = 0;
while((len = inputStream.read(data)) != -1){
total_length +=len;
//进度条刻度的计算
int value = (int)(total_length/(float)file_length);
publishProgress(value); //将刻度发布出去
outputStream.write(data,0,len); //将获得的图片发送出去
}
byte [] result = outputStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(result,0,result.length);
}
}catch (Exception e) {
e.printStackTrace();
}
finally {
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
dialog.dismiss();
imageView.setImageBitmap(bitmap);
}
}
}
2、实现效果