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);
} } }

Android_AsyncTask_DownloadImg的更多相关文章

随机推荐

  1. Jquery+asp.net后台数据传到前台js进行解析的方法

    所以在解析后台数据的时候,我们需要根据后台的数据情况,特殊处理和对待. 我这里后台用的是asp.net提供的wcf服务,也有ashx一般处理程序.大致原理差不多. C#中我们经常用的对象,有实体对象比 ...

  2. FZU 2125 简单的等式

    Problem Description 现在有一个等式如下:x^2+s(x,m)x-n=0.其中s(x,m)表示把x写成m进制时,每个位数相加的和.现在,在给定n,m的情况下,求出满足等式的最小的正整 ...

  3. C#操作Json(转)

    原文:http://wenku.baidu.com/link?url=3dlqKdF26ZdQIAcX9jvP2ZYEtl3J0sKOV8XuHQI0Rz4SjB9S46nqmGiMXUVQa_1Pm ...

  4. [原][Android]All WebView methods must be called on the same thread.

    问题 webView调用JS出错. class TestJS {         ......         public TestJS(){         }                   ...

  5. 深入浅出 JavaScript 变量、作用域和内存 v 0.5

    本文主要从原理入手分享变量和作用域的相关知识,最后结合本文所分享知识,再次深入了解下闭包的运行原理. 主要参考<JS高级程序设计> <JS权威指南> <高性能 JS> ...

  6. codeforce--600D - Area of Two Circles' Intersection

    题意:求相交圆的面积.借鉴大神代码,精度超高. #include <fstream> #include <iostream> #include <string> # ...

  7. uvalive 4973 Ardenia

    题意:给出空间两条线段,求距离. 注意输出格式! #include<cstdio> #include<cmath> #include<algorithm> usin ...

  8. 50道经典的JAVA编程题 (16-20)

    50道经典的JAVA编程题 (16-20),用了快一个下午来做这10道题了,整理博客的时间貌似大于编程的时间啊..哈哈 [程序16]Nine.java 题目:输出9*9口诀. 1.程序分析:分行与列考 ...

  9. hadoop cdh 4.5的安装配置

    春节前用的shark,是从github下载的源码,自己编译.shark的master源码仅支持hive 0.9,支持hive 0.11的shark只是个分支,不稳定,官方没有发布release版,在使 ...

  10. HW7.11

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...