java根据url抓取并生成缩略图
复代码代码如下:
공개 정적 비트맵 loadImageFromUrl(String url, int sc) {
URLm;
입력스트림 i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream 출력 = null;
바이트 isBuffer[] = 새 바이트[1024];
if (url == null)
null을 반환;
노력하다 {
m = 새 URL(url);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i, 1024 * 4);
아웃 = 새로운 ByteArrayOutputStream();
int len = 0;
while ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
종료.닫기();
bis.close();
} 잡기(MalformedURLException e1) {
e1.printStackTrace();
null을 반환;
} 잡기(IOException e) {
e.printStackTrace();
}
if (out == null)
null을 반환;
byte[] 데이터 = out.toByteArray();
BitmapFactory.Options 옵션 = 새로운 BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inJustDecodeBounds = false;
int be = (int) (options.outHeight / (float) sc);
if (be <= 0) {
= 1;
} else if (be > 3) {
= 3;
}
options.inSampleSize = 수;
비트맵 bmp = null;
노력하다 {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
} 잡기(OutOfMemoryError e) {
// TODO: 예외 처리
시스템.gc();
bmp = 널;
}
bmp를 반환;
}