Android 异步加载图片-AsyncTask
fmms
13年前
AsyncTask定义了三种泛型类型 Params,Progress和Result。
•Params 启动任务执行的输入参数,比如HTTP请求的URL。
•Progress 后台任务执行的百分比。
•Result 后台执行任务最终返回的结果,比如String。
Activity
/* * Android 异步任务AsyncTask * AsyncTask定义了三种泛型类,Params,Progress,Result */ package rw.ansync; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; public class AsynaTask_TestActivity extends Activity { /** Called when the activity is first created. */ private EditText editText; private Button button; private ProgressBar progressBar; private ImageView imageView; private static final String URL_STRING="http://photocdn.sohu.com/20110927/Img320705637.jpg"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText=(EditText) findViewById(R.id.editText1); editText.setText(URL_STRING); button=(Button) findViewById(R.id.button1); progressBar=(ProgressBar) findViewById(R.id.progressBar1); imageView=(ImageView) findViewById(R.id.imageView1); button.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub MyAsynaTask mTask=new MyAsynaTask(imageView, progressBar,button); mTask.execute(URL_STRING); } } }MyAsynaTask.java
package rw.ansync; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import android.R.integer; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.IInterface; import android.util.Log; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; public class MyAsynaTask extends AsyncTask加上权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>{ private ImageView imageView; private ProgressBar progressBar; private Button button; public MyAsynaTask( ImageView imageView, ProgressBar progressBar,Button button) { super(); this.imageView = imageView; this.progressBar = progressBar; this.button=button; } @Override protected void onPreExecute() { // TODO Auto-generated method stub /* * 改方法在执行实际的后台操作时被UI线程调用,可以在该方法中做一些准备工作,比如 * Toast.makeText(context, "准备下载", Toast.LENGTH_LONG).show(); */ super.onPreExecute(); } @Override protected Bitmap doInBackground(String... params) {//输入编变长的可变参数 和UI线程中的Asyna.execute()对应 // TODO Auto-generated method stub /* * 该方法在OnpreExecute执行以后马上执行,改方法执行在后台线程当中,负责耗时的计算,可以调用publishProcess方法来实时更新任务进度 */ Bitmap bitmap=null; try { URL url=new URL(params[0]); Log.i("------------->", url+""); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.connect(); int MAX=connection.getContentLength(); progressBar.setMax(MAX); InputStream inputStream=connection.getInputStream(); ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); /* * 为了显示进度条,每接受1024字节要求更新一次UI,为了看效果 */ byte []buf=new byte[1024]; int len = 0; int processBarNum=0; while( (len=inputStream.read(buf))!=-1){ outputStream.write(buf, 0, len); processBarNum+=len; publishProgress(processBarNum);//通知要更新processBar } bitmap=BitmapFactory.decodeByteArray(outputStream.toByteArray(),0, MAX); inputStream.close(); } catch (Exception e) { // TODO: handle exception } return bitmap; } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub /* * 当publichProcess 呗调用以后,UI线程将调用这个有方法在界面上展示任务的情况,比如一个额进度条。这里是更新进度条 */ int value=values[0]; progressBar.setProgress(value); super.onProgressUpdate(values); } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub /* * 在doInbackground执行完成以后,onPostExecute将被调用,后台的结果将返回给UI线程,将获得图片显示出来 */ imageView.setImageBitmap(result); super.onPostExecute(result); } }
转自:http://blog.csdn.net/rwyz1314/article/details/6841639