上一篇文章《AsyncTask异步任务线程框架(一)》中,基本上实现了从网络上下载图片的功能,现在对此程序进行优化——ProgressDialog的刻度显示。
xml代码不变。
MainActivity.java代码如下:
package com.example.asynctask_dowmloadimage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private Button button;
private ImageView imageView;
private final String str = "http://img0.bdstatic.com/img/image/shouye/bzsjb-12326925706.jpg";
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
imageView = (ImageView) findViewById(R.id.imageView1);
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("提示信息");
dialog.setMessage("图片下载中...");
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new MyAsync().execute(str);
}
});
}
class MyAsync extends AsyncTask<String, Integer, byte[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected byte[] doInBackground(String... params) {
byte[] result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
byte[] data = new byte[1024];
InputStream inputStream = null;
long file_length = 0;
long total_length = 0;
int length = 0;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
file_length = httpResponse.getEntity().getContentLength();
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// result =
// EntityUtils.toByteArray(httpResponse.getEntity());
inputStream = httpResponse.getEntity().getContent();
while ((length = inputStream.read(data)) != -1) {
total_length += length;
int prograssBar = (int) (total_length
/ (float) file_length * 100);
publishProgress(prograssBar);
outputStream.write(data, 0, length);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
result = outputStream.toByteArray();
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(byte[] result) {
super.onPostExecute(result);
// Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0,
// result.length);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(result, 0,
result.length));
dialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}