Android使用HttpClient下载图片


在前一章中讲的是Android使用HttpURLConnection下载图片(),这一章使用HttpClient下载图片

HttpURLConnection与HttpClient的区别:

HttpClient是个很不错的开源框架(org.appache.http),封装了访问http的请求头,参数,内容体,响应等等,使用起来更方面更强大。

 HttpURLConnection是java的标准类,可以实现简单的基于URL请求、响应功能,什么都没封装,用起来太原始,比如重访问的自定义,以及一些高级功能等。

还是在上一章的基础上添加HttpClient

Android使用HttpClient下载图片

--------------------------------------分割线 --------------------------------------

本文完整Demo下载:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2014年资料/5月/17日/Android使用HttpClient下载图片

下载方法见

--------------------------------------分割线 --------------------------------------

Adroid 4.0 HttpURLConnection抛异常解决方法

node.js+Android(使用HttpURLConnection和HttpClient)实现文件上传

在Android上用HttpURLConnection获取网页内容

关键下载代码

/**
  * 通过Get获取网页内容
  *
  * @param url
  *            如:http://www.linuxboy.net/upload/2013_09/130928195459231.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);

  // 如果服务器响应的是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://www.linuxboy.net/upload/2014_03/14030211418164.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);
  }

 }
}

如果图片较大可能会下载失败

 

相关内容