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. 【转】Qt Creator在Windows上的调试器安装与配置

    https://www.librehat.com/qt-creator-on-windows-debugger-installation-and-configuration/

  2. 设计模式_Facade_门面模式

    形象例子: 我有一个专业的Nikon相机,我就喜欢自己手动调光圈.快门,这样照出来的照片才专业,但MM可不懂这些,教了半天也不会.幸好相机有Facade设计模式,把相机调整到自动档,只要对准目标按快门 ...

  3. 【译】 AWK教程指南 附录E-正则表达式

    为什么要使用正则表达式 UNIX 中提供了许多 指令 和 tools,它们具有在文件中 查找(Search)字串或替换(Replace)字串 的功能.像 grep, vi , sed, awk,... ...

  4. jdk源码调试功能

    JDK源码重新编译——支持eclipse调试JDK源码--转载 最近在研究jdk源码,发现debug时无法查看源码里的变量值. 因为sun提供的jdk并不能查看运行中的局部变量,需要重新编译一下rt. ...

  5. SQL Server: Difference Between Locking, Blocking and Dead Locking

    Like ever, today’s article of Pinal Dave was interesting and informative. After, our mutual discussi ...

  6. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  7. 转载:MATLAB画图常用调整代码

    %单y轴 plot(t*1e+,abs(iGG)/max(abs(iGG)),); axis([-,,,]) xlabel('时间/ns'); ylabel('幅度/a.u.'); ,'FontNam ...

  8. Java 开发@ JDBC链接SQLServer2012

    下面请一字一句地看,一遍就设置成功,比你设置几十遍失败,费时会少得多. 首先,在连接数据库之前必须保证SQL Server 2012是采用SQL Server身份验证方式而不是windows身份验证方 ...

  9. Java修饰符关键词大全

    所以我以此主题写了这篇文章.这也是一个可用于测试你的计算机科学知识的面试问题. Java修饰符是你添加到变量.类和方法以改变其含义的关键词.它们可分为两组: 访问控制修饰符 非访问修饰符 让我们先来看 ...

  10. A Tour of Go Buffered Channels

    Channels can be buffered. Provide the buffer length as the second argument to make to initialize a b ...