1、创建项目MyApplication
2、添加访问网络的权限
在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />,该文件最终内容是:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.letian.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3、修改布局文件activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取图片" />
<ImageView
android:id="@+id/imageview01"
android:layout_below="@id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
4、修改MyActivity.java
package com.example.letian.myapplication;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.InputStream;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button btn01 = (Button) this.findViewById(R.id.button01);
final ImageView iv01 = (ImageView) this.findViewById(R.id.imageview01);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bitmap bitmap=(Bitmap)msg.obj;
iv01.setImageBitmap(bitmap);
}
};
final Runnable runnable = new Runnable(){
@Override
public void run() {
HttpGet getRequest = new HttpGet("http://static.oschina.net/uploads/user/470/940565_100.jpg?t=1400854721000");
getRequest.addHeader("User-Agent", " Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0");
try {
HttpResponse response = new DefaultHttpClient().execute(getRequest);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream imageStream = response.getEntity().getContent();
Bitmap result = BitmapFactory.decodeStream(imageStream);
Message msg = new Message();
msg.obj=result;
handler.sendMessage(msg);
} else {
throw new Exception("读取失败");
}
} catch (Exception e) {
try {
throw new Exception("读取失败:\n"+e.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
};
btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(runnable).start();
}
});
}
}
上面的runnable中HTTP请求使用了已经封装好的HttpGet,也可以使用HttpURLConnection,如下:
final Runnable runnable = new Runnable(){
@Override
public void run() {
try {
URL imageUrl = new URL("http://static.oschina.net/uploads/user/470/940565_100.jpg?t=1400854721000");
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 ...");
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
Message msg = new Message();
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
};
5、效果
![](http://static.oschina.net/uploads/space/2014/0929/145209_s1Yx_940565.png)
获取图片:
![](http://static.oschina.net/uploads/space/2014/0929/145229_y0Vw_940565.png)
参考:
http://stackoverflow.com/questions/13012702/android-sdk-obtain-image-from-http-get
http://blog.csdn.net/nasebanaru/article/details/17201783