layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:maxHeight="300dp" android:maxWidth="300dp" android:adjustViewBounds="true"/> <Button android:id="@+id/btn_download" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onDownLoadImg" android:text="点击下载图片" android:textSize = "20sp"/> </LinearLayout>
main.java
package com.example.day07_asynctask_downloadimg; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.widget.ImageView; /* * 下载图片: * 1.初始化控件 * 2.设置下载图片按钮监听 * 2.1创建一个异步任务类,实现其抽象方法 * 2.2开启异步任务开始下载 * 在子线程中进行进行耗时操作下载图片 * 1)创建HttpCient对象 * ` 2)创建HttpGet对象传入 * 3)执行请求获得HttpResponse对象 * 4)获得响应码 * 5)判断响应码,成功,获得实体对象httpEntity * 6)通过HttpEntity对象的getContent()方法得到读取流 * 7)封装成BitMap对象,并传回给主线程 * 在主线程中显示图片 * * */ public class MainActivity extends Activity { private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); } //2.设置下载图片按钮监听 public void onDownLoadImg(View v){ //new MyAsyncTask().execute("http://c.hiphotos.baidu.com/image/h%3D200/sign=8cbc53a04ded2e73e3e9812cb700a16d/f7246b600c338744513e9358560fd9f9d72aa01f.jpg"); } // 2.1创建一个异步任务类,实现其抽象方法 class MyAsyncTask extends AsyncTask<String, Void, Bitmap>{ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Bitmap doInBackground(String... params) { try { DefaultHttpClient defaultCLient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); HttpResponse response = defaultCLient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if(code == 200){ HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); Bitmap result = BitmapFactory.decodeStream(content); return result; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); image.setImageBitmap(result); } } }