从网络上获取图片的两种方式讲解:thread+handle和AsyncTask方式

    从网络上获取图片是一个比较耗时的操作,放在主线程会导致阻塞主线程,响应超时,所以我们不能把它放在主线程里操作,必须放在一个子线程里,我打算采用两种方式去实现。1、采用thread去获取图片,获取到后通过handle把消息发送到与主线程绑定的消息队列中(也就是主线程的loop)。2、采用AsyncTask方式,也称异步任务,这个类其实把子线程和handle的处理方式进行了封装,建议开发者多用异步任务来处理,但是对thread和handle的原理一定要搞清楚。

效果图如下:

下面先采用第一种方式:

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


源码下载


猜你喜欢

转载自blog.csdn.net/hanguohui3/article/details/14449005