java根据url抓取并生成缩略图
复制代码代码如下:
öffentliche statische Bitmap loadImageFromUrl(String url, int sc) {
URL m;
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out = null;
Byte isBuffer[] = neues Byte[1024];
if (URL == null)
null zurückgeben;
versuchen {
m = neue URL(URL);
i = (InputStream) m.getContent();
bis = new BufferedInputStream(i, 1024 * 4);
out = new ByteArrayOutputStream();
int len = 0;
while ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
out.close();
bis.close();
} Catch (MalformedURLException e1) {
e1.printStackTrace();
null zurückgeben;
} Catch (IOException e) {
e.printStackTrace();
}
if (out == null)
null zurückgeben;
byte[] data = out.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inJustDecodeBounds = false;
int be = (int) (options.outHeight / (float) sc);
if (sei <= 0) {
sei = 1;
} else if (be > 3) {
sei = 3;
}
options.inSampleSize = be;
Bitmap bmp = null;
versuchen {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
} Catch (OutOfMemoryError e) {
// TODO: Ausnahme behandeln
System.gc();
bmp = null;
}
bmp zurückgeben;
}