java根据url抓取并生成了一点
复制代码代码如下:
公共静态位图 loadImageFromUrl(String url, int sc) {
网址 m;
输入流 i = null;
BufferedInputStream之二=空;
ByteArrayOutputStream 输出 = null;
字节 isBuffer[] = 新字节[1024];
如果(网址==空)
返回空值;
尝试 {
m = 新的 URL(url);
i = (InputStream) m.getContent();
双 = 新 BufferedInputStream(i, 1024 * 4);
输出 = 新的 ByteArrayOutputStream();
int 长度 = 0;
while ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
关闭();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
返回空值;
} catch (IOException e) {
e.printStackTrace();
}
如果(输出==空)
返回空值;
byte[] 数据 = out.toByteArray();
BitmapFactory.Options 选项 = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inJustDecodeBounds = false;
int be = (int) (options.outHeight / (float) sc);
如果(是 <= 0){
是 = 1;
} 否则如果 (be > 3) {
是 = 3;
}
options.inSampleSize = be;
位图 bmp = null;
尝试 {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回大概
} catch (OutOfMemoryError e) {
// TODO: 处理异常
系统.gc();
bmp = 空;
}
返回bmp;
}