在前一章中講的是Android使用HttpURLConnection下載圖片(http://www.linuxidc.com/Linux/2014-05/101854.htm),這一章使用HttpClient下載圖片
HttpURLConnection與HttpClient的區別:
HttpClient是個很不錯的開源框架(org.appache.http),封裝了訪問http的請求頭,參數,內容體,響應等等,使用起來更方面更強大。
HttpURLConnection是java的標准類,可以實現簡單的基於URL請求、響應功能,什麼都沒封裝,用起來太原始,比如重訪問的自定義,以及一些高級功能等。
還是在上一章的基礎上添加HttpClient
--------------------------------------分割線 --------------------------------------
本文完整Demo下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2014年資料/5月/17日/Android使用HttpClient下載圖片
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
--------------------------------------分割線 --------------------------------------
Adroid 4.0 HttpURLConnection拋異常解決方法 http://www.linuxidc.com/Linux/2013-03/81085.htm
node.js+Android(使用HttpURLConnection和HttpClient)實現文件上傳 http://www.linuxidc.com/Linux/2012-02/53532p2.htm
在Android上用HttpURLConnection獲取網頁內容 http://www.linuxidc.com/Linux/2011-08/41211.htm
關鍵下載代碼
/**
* 通過Get獲取網頁內容
*
* @param url
* 如:http://www.linuxidc.com/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.linuxidc.com/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);
}
}
}
如果圖片較大可能會下載失敗