Android_AsyncTask_DownloadImg
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的更多相关文章
随机推荐
- 转换Json格式帮助类
using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Sy ...
- selenium运行chrome去掉command -line flag
每次驱动chrome浏览器都会出现这玩意,比较烦人··想办法去掉了它: ChromeOptions options = new ChromeOptions();options.addArguments ...
- python 中 struct 用法
下面就介绍这个模块中的几个方法. struct.pack():我的理解是,python利用 struct模块将字符(比如说 int,long ,unsized int 等)拆成 字节流(用十六进制表示 ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- 把公共cpp包含到cocos2d-x内部编译的方法。。
找到cocos2d-x-3.0alpha0-pre\extensions\Android.mk文件,把自定义的cpp文件加进去即可..如果是其它系统就进相应的目录,找到配置文件添加即可..
- 【Hadoop学习】Apache HBase项目简介
正在撰写,稍后来访……
- 如何用Java编写一段代码引发内存泄露
本文来自StackOverflow问答网站的一个热门讨论:如何用Java编写一段会发生内存泄露的代码. Q:刚才我参加了面试,面试官问我如何写出会发生内存泄露的Java代码.这个问题我一点思路都没有, ...
- Spark的部署方式
1.Spark的应用程序部署 2.Spark的集群部署
- Java文件合并
文件分割与合并是一个常见需求,比如:上传大文件时,可以先分割成小块,传到服务器后,再进行合并.很多高大上的分布式文件系统(比如:google的GFS.taobao的TFS)里,也是按block为单位, ...
- Oracle-PLSQL Developer使用笔记
1.新建菜单 command window ---->命令行,执行sql语句 sql window ---->执行sql语句,可导出CSV,TSV,HTML,XML等类型文件 report ...