在上一章中谈到Android采用HttpURLConnection下载图片,本章使用HttpClient下载图片
HttpURLConnection与HttpClient的差别:
HttpClient是个非常不错的开源框架(org.appache.http),封装了訪问http的请求头,參数,内容体。响应等等,使用起来更方面更强大。
HttpURLConnection是java的标准类,能够实现简单的基于URL请求、响应功能,什么都没封装。用起来太原始。比方重訪问的自己定义,以及一些高级功能等。
还是在上一章的基础上加入HttpClient
关键下载代码
/** * 通过Get获取网页内容 * * @param url * 如:http://preview.quanjing.com/is002/ev601-025.jpg * @return * @throws ClientProtocolException * @throws IOException * @date 2014.05.10 */ public static Bitmap getHttpGetBitmap(String url) throws ClientProtocolException, IOException { Bitmap bitmap = null; // 新建一个默认的连接 HttpClient client = new DefaultHttpClient(); // 新建一个Get方法 HttpGet get = new HttpGet(url); // 得到网络的回应 HttpResponse response = client.execute(get); // 假设server响应的是OK的话!if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); bitmap = BitmapFactory.decodeStream(is); is.close(); } return bitmap; }
訪问互联网权限
<uses-permission android:name="android.permission.INTERNET" />Activity下载代码
package com.dzt.downloadimage; import java.io.IOException; import java.net.MalformedURLException; import org.apache.http.client.ClientProtocolException; import android.app.Activity; import android.graphics.Bitmap; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import com.dzt.downloadimage.utils.HttpUtils; public class MainActivity extends Activity implements OnClickListener { private Bitmap mDownloadImage = null; private ImageView image = null; private downloadImageTask task; private boolean _isExe = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidgets(); task = new downloadImageTask(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); if (_isExe) { task.cancel(true); // 取消操作 } } private void initWidgets() { image = (ImageView) findViewById(R.id.img); Button btn = (Button) findViewById(R.id.download_btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.download_btn: if (!_isExe) { task.execute("http://preview.quanjing.com/is002/ev601-025.jpg"); // 运行异步操作 _isExe = true; } break; default: break; } } class downloadImageTask extends AsyncTask<String, Integer, Boolean> { @Override protected Boolean doInBackground(String... params) { // TODO Auto-generated method stub System.out.println("[downloadImageTask->]doInBackground " + params[0]); // try { // mDownloadImage = HttpUtils.getNetWorkBitmap(params[0]); // } catch (MalformedURLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } try { mDownloadImage = HttpUtils.getHttpGetBitmap(params[0]); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } // 下载完毕回调 @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub image.setImageBitmap(mDownloadImage); System.out.println("result = " + result); super.onPostExecute(result); } // 更新进度回调 @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } } }假设图片较大可能会下载失败
Demo:http://download.csdn.net/detail/deng0zhaotai/7326167
版权声明:本文博主原创文章,博客,未经同意不得转载。