源代码如下
package com.example.asynctask;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.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class MainActivity extends AppCompatActivity { Button button; ImageView imageView; ProgressDialog progressDialog; String IMAGE_PATH="http://image.baidu.com/search/redirect?tn=redirect&word=j&juid=51AEAB&sign=cibzwikaba&url=http%3A%2F%2Flife.northnews.cn%2F2015%2F0515%2F1927051_20.shtml&objurl=http%3A%2F%2Fupload.northnews.cn%2F2015%2F0515%2F1431655212431.jpg"; @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.image); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("提示信息"); progressDialog.setCancelable(false); progressDialog.setMessage("正在下载中,请稍后。。。"); progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new MyAsyncTask().execute(IMAGE_PATH); } }) ; } public class MyAsyncTask extends AsyncTask{ @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected byte[] doInBackground(String... strings) { byte[] image = new byte[]{}; HttpURLConnection connection = null; InputStream inputStream = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Log.i("test"," ===== "); try { URL url = new URL(strings[0]); connection = (HttpURLConnection) url.openConnection(); if (connection.getResponseCode()==200){ file_length = connection.getContentLength(); long totle_length = 0; int length = 0; byte[] data = new byte[1024]; inputStream = connection.getInputStream(); while (-1 !=(length = inputStream.read(data))){ totle_length += length; byteArrayOutputStream.write(data,0,length); int progress = ((int)(totle_length*100/(float)file_length)); publishProgress(progress); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } Log.i("test","-----"); image = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { connection.disconnect(); }return image; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); progressDialog.setProgress(values[0]);} @Override protected void onPostExecute(byte[] bytes) { super.onPostExecute(bytes); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length); imageView.setImageBitmap(bitmap); progressDialog.dismiss(); } }}
存在的问题:
1.不是所有代码都简单易懂 ,代码的变量名没有注意到,有的可能没加
2.不太符合编程规范。大括号的位置有的随意乱放,只要没有出现错误就行。基本没有注释
3.可能存在多余的代码
4.代码没有尽可能的模块化