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;
}