java根据url抓取并生成缩略图
复制代码代码如下:
bitmap estático público loadImageFromUrl(String url, int sc) {
URLm;
InputStream i = null;
BufferedInputStream bis = nulo;
ByteArrayOutputStream out = null;
byte isBuffer[] = novo byte[1024];
if (url == nulo)
retornar nulo;
tentar {
m = nova URL(url);
i = (InputStream) m.getContent();
bis = novo BufferedInputStream(i, 1024 * 4);
saída = new ByteArrayOutputStream();
int len = 0;
enquanto ((len = bis.read(isBuffer)) != -1) {
out.write(isBuffer, 0, len);
}
out.close();
bis.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
retornar nulo;
} catch (IOException e) {
e.printStackTrace();
}
if (fora == nulo)
retornar nulo;
byte[] dados = out.toByteArray();
Opções BitmapFactory.Options = new BitmapFactory.Options();
opções.inJustDecodeBounds = verdadeiro;
BitmapFactory.decodeByteArray(dados, 0, data.length, opções);
opções.inJustDecodeBounds = falso;
int be = (int) (options.outHeight / (float) sc);
if (ser <= 0) {
ser = 1;
} senão if (ser> 3) {
ser = 3;
}
opções.inSampleSize = ser;
Bitmap bmp = null;
tentar {
bmp = BitmapFactory.decodeByteArray(dados, 0, data.length, opções); // 返回缩略图
} catch (OutOfMemoryError e) {
//TODO: trata exceção
System.gc();
bmp = nulo;
}
retornar bmp;
}