HttpURLConnection方式
public Bitmap getImageBitmap(String url) {
URL imgUrl = null;
Bitmap bitmap = null;
try {
imgUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imgUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
HttpClient方式
public Bitmap getImageBitmap(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse resp = httpclient.execute(httpget);
// 判断是否正确执行
if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
// 将返回内容转换为bitmap
HttpEntity entity = resp.getEntity(