从网络上获取图片的两种方式讲解:thread+handle和AsyncTask方式
2013-11-07 12:43
495 查看
从网络上获取图片是一个比较耗时的操作,放在主线程会导致阻塞主线程,响应超时,所以我们不能把它放在主线程里操作,必须放在一个子线程里,我打算采用两种方式去实现。1、采用thread去获取图片,获取到后通过handle把消息发送到与主线程绑定的消息队列中(也就是主线程的loop)。2、采用AsyncTask方式,也称异步任务,这个类其实把子线程和handle的处理方式进行了封装,建议开发者多用异步任务来处理,但是对thread和handle的原理一定要搞清楚。
效果图如下:
下面先采用第一种方式:
AsyncTask方式:
源码下载
效果图如下:
下面先采用第一种方式:
public class MainActivity extends Activity { private Button button; private ImageView imageView; private ProgressBar progressBar; private MyHandle handle; private final static int CODE =1; private final static String URL = "http://pic10.nipic.com/20101016/5311590_152428004670_2.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageview); progressBar = (ProgressBar)findViewById(R.id.progress); handle = new MyHandle(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub progressBar.setVisibility(View.VISIBLE); new DownThread(URL).start(); } }); } public class DownThread extends Thread{ String url2; public DownThread(String url){ this.url2 = url; } @Override public void run() { InputStream inputStream = null; HttpURLConnection connection = null; try { URL url = new URL(url2); //OutputStream outputStream = null; connection = (HttpURLConnection) url.openConnection(); if(connection.getResponseCode() == 200){ inputStream = connection.getInputStream(); } Bitmap bitmap = BitmapFactory.decodeStream(inputStream); Message msg = Message.obtain(); msg.obj = bitmap; msg.arg1 = CODE; handle.sendMessage(msg); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection!=null){ connection.disconnect(); } } } } private class MyHandle extends Handler{ public MyHandle(){ } @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.arg1) { case CODE: Bitmap bitmap = (Bitmap)msg.obj; imageView.setVisibility(View.VISIBLE); progressBar.setVisibility(View.GONE); imageView.setImageBitmap(bitmap); break; } } }
AsyncTask方式:
public class MainActivity extends Activity { private Button button; private ProgressBar progressBar; private ImageView imageView; private final static String URL = "http://pic10.nipic.com/20101016/5311590_152428004670_2.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); imageView = (ImageView) findViewById(R.id.imageview); progressBar = (ProgressBar) findViewById(R.id.progress); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new DownloadTast().execute(URL); } }); } private class DownloadTast extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { progressBar.setVisibility(View.VISIBLE); } @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub String urls = params[0]; InputStream inputStream = null; Bitmap bitmap = null; HttpURLConnection connection =null; try { URL url = new URL(urls); connection = (HttpURLConnection) url .openConnection(); if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection !=null){ connection.disconnect(); } } return bitmap; } @Override protected void onPostExecute(Bitmap result) { imageView.setVisibility(View.VISIBLE); imageView.setImageBitmap(result); progressBar.setVisibility(View.GONE); } }
源码下载
相关文章推荐
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- Android APP与媒体存储服务的交互
- android 多线程技术应用
- Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
- Android数据类型之间相互转换系统介绍
- 通过Android trace文件分析死锁ANR实例过程
- Android实现左右滑动效果的方法详解
- Android 各国语言缩写及简称详细介绍
- Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
- android IPC之binder通信机制
- Android应用开发中模拟按下HOME键的效果(实现代码)
- Android 读写文件方法汇总
- Android如何禁止横屏竖屏的变换