java根据url抓取并生成缩略image
复制代码代码如下:
โหลดบิตแมปแบบคงที่สาธารณะ loadImageFromUrl (URL สตริง, int sc) {
URL ม.;
อินพุตสตรีม i = null;
BufferedInputStream ทวิ = null;
ByteArrayOutputStream ออก = null;
ไบต์ isBuffer[] = ไบต์ใหม่ [1024];
ถ้า (url == null)
กลับเป็นโมฆะ;
พยายาม {
ม. = URL ใหม่ (URL);
i = (InputStream) m.getContent();
ทวิ = BufferedInputStream ใหม่ (i, 1024 * 4);
ออก = ByteArrayOutputStream ใหม่ ();
อินท์เลน = 0;
ในขณะที่ ((len = bis.read (isBuffer)) != -1) {
ออก.write(isBuffer, 0, len);
-
ออก.ปิด();
ทวิ.ปิด();
} จับ (MalformedURLException e1) {
e1.printStackTrace();
กลับเป็นโมฆะ;
} จับ (IOException จ) {
e.printStackTrace();
-
ถ้า (ออก == null)
กลับเป็นโมฆะ;
ไบต์ [] ข้อมูล = out.toByteArray ();
ตัวเลือก BitmapFactory.Options = BitmapFactory.Options ใหม่ ();
options.inJustDecodeBounds = จริง;
BitmapFactory.decodeByteArray (ข้อมูล, 0, data.length, ตัวเลือก);
options.inJustDecodeBounds = เท็จ;
int be = (int) (options.outHeight / (float) sc);
ถ้า (เป็น <= 0) {
เป็น = 1;
} อื่นถ้า (เป็น > 3) {
เป็น = 3;
-
options.inSampleSize = เป็น;
บิตแมป bmp = null;
พยายาม {
bmp = BitmapFactory.decodeByteArray (ข้อมูล, 0, data.length, ตัวเลือก); // 返回缩略image
} จับ (OutOfMemoryError จ) {
// สิ่งที่ต้องทำ: จัดการข้อยกเว้น
System.gc();
bmp = โมฆะ;
-
กลับ bmp;
-