在Android中显示一张网络图片其实是超级简单的,下面就一个非常简单的例子:
String urlString ="http://s7.sinaimg.cn/middle/9b82a8c54c10ecacbb686&960";
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.imageview01);
try {
imageView.setImageBitmap(returnBitMap(urlString));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Bitmap returnBitMap(String url) throws IOException{
Bitmap bm = null;
URL myFileUrl = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream in = conn.getInputStream();
bm = BitmapFactory.decodeStream(in);
in.close();
return bm;
}