在高级的android 版本上,加载图片
/**
* 从网络中获取图片,以流的形式返回
*
* @return
*/
public static InputStream getImageViewInputStream(String URL_PATH)
throws IOException {
InputStream inputStream = null;
URL url = new URL(URL_PATH); // 服务器地址
if (url != null) {
// 打开连接
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);// 设置网络连接超时的时间为3秒
httpURLConnection.setRequestMethod("GET"); // 设置请求方法为GET
httpURLConnection.setDoInput(true); // 打开输入流
// int responseCode = httpURLConnection.getResponseCode(); //
// 获取服务器响应值
// if (responseCode == HttpURLConnection.HTTP_OK) { //正常连接
inputStream = httpURLConnection.getInputStream(); // 获取输入流
// }
}
return inputStream;
}
该函数必须要在新开的一个线程中调用,否则报错
调用代码
new Thread(new Runnable() {
@Override
public void run() {
InputStream inputStream = null;
try {
inputStream = getImageViewInputStream(NetWorkApiContext.URL_TEMP
+ imgL[i]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(inputStream,null,opt);
Message msg=new Message();
msg.what=MSG_FINISH;
msg.obj=bitmap;
handle.sendMessage(msg);
}
}).start();