<h2><pre name="code" class="html" style="font-weight: bold; font-size: 24px;">
一、注意点:连接对象获取,请求方法“GET”,资源获取超时设置,建立连接,通过连接获取输入流,采用谷歌API:BitmapFactory得到图片对象Bitmap。
public Bitmap getInternetPicture(String UrlPath) {
Bitmap bm = null;
// 1、确定网址
// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg
String urlpath = UrlPath;
// 2、获取Uri
try {
URL uri = new URL(urlpath);
// 3、获取连接对象、此时还没有建立连接
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
// 4、初始化连接对象
// 设置请求的方法,注意大写
connection.setRequestMethod("GET");
// 读取超时
connection.setReadTimeout(5000);
// 设置连接超时
connection.setConnectTimeout(5000);
// 5、建立连接
connection.connect();
// 6、获取成功判断,获取响应码
if (connection.getResponseCode() == 200) {
// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中
InputStream is = connection.getInputStream();
// 8、从流中读取数据,构造一个图片对象GoogleAPI
bm = BitmapFactory.decodeStream(is);
// 9、把图片设置到UI主线程
// ImageView中,获取网络资源是耗时操作需放在子线程中进行,通过创建消息发送消息给主线程刷新控件;
Log.i("", "网络请求成功");
} else {
Log.v("tag", "网络请求失败");
bm = null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
二、 同时要注意网络操作需在子线程操作,以免引起主线程阻塞,影响用途体验,同时采用handler消息机制进行参数处理,刷新UI控件。
public void onClick(View v){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String urlpath = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";
Bitmap bm = getInternetPicture(urlpath);
Message msg = new Message();
// 把bm存入消息中,发送到主线程
msg.obj = bm;
handler.sendMessage(msg);
}
}).start();
}</span>
三、 主线程处理消息队列中的消息,并刷新相应UI控件
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
ImageView imgView = (ImageView) findViewById(R.id.internet_imageview);
imgView.setImageBitmap((Bitmap) msg.obj);
};
};</span>
四、获取网络图片,采用缓存保存文件
<span style="font-size:18px;">public Bitmap getInternetPicture(String UrlPath) {
Bitmap bm = null;
// 1、确定网址
// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg
String urlpath = UrlPath;
// 2、获取Uri
try {
URL uri = new URL(urlpath);
// 3、获取连接对象、此时还没有建立连接
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
// 4、初始化连接对象
// 设置请求的方法,注意大写
connection.setRequestMethod("GET");
// 读取超时
connection.setReadTimeout(5000);
// 设置连接超时
connection.setConnectTimeout(5000);
// 5、建立连接
connection.connect();
// 6、获取成功判断,获取响应码
if (connection.getResponseCode() == 200) {
// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中
InputStream is = connection.getInputStream();
// 8、开启文件输出流,把读取到的字节写到本地缓存文件
File file = new File(getCacheDir(), getFileName(urlpath));
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.close();
is.close();
//9、 通过图片绝对路径,创建Bitmap对象
bm = BitmapFactory.decodeFile(file.getAbsolutePath());
Log.i("", "网络请求成功");
} else {
Log.v("tag", "网络请求失败");
bm = null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
public String getFileName(String path) {
int index = path.lastIndexOf("/");
return path.substring(index + 1);
}
}</span>