项目常有获取服务端图片的需求,自己做了下整理,希望帮助的大家和自己!
/**
* 获取服务器上的图片尺寸
*/
public static int[] getImgWH(String urls) throws Exception {
URL url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap image = BitmapFactory.decodeStream(is);
int srcWidth = image.getWidth(); // 源图宽度
int srcHeight = image.getHeight(); // 源图高度
int[] imgSize = new int[2];
imgSize[0] = srcWidth;
imgSize[1] = srcHeight;
//释放资源
image.recycle();
is.close();
conn.disconnect();
return imgSize;
}
异常自己处理即可。