javaベースurl抓取并生成缩略図
复制番号代番号次のように:
public static BitmaploadImageFromUrl(String url, int sc) {
URL m;
入力ストリーム i = null;
BufferedInputStream ビス = null;
ByteArrayOutputStream 出力 = null;
バイト isBuffer[] = 新しいバイト [1024];
if (url == null)
null を返します。
試す {
m = 新しい URL(url);
i = (入力ストリーム) m.getContent();
bis = 新しい BufferedInputStream(i, 1024 * 4);
out = 新しい 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 を返します。
} キャッチ (IOException e) {
e.printStackTrace();
}
if (out == null)
null を返します。
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);
if (be <= 0) {
be = 1;
else if (be > 3) {
be = 3;
}
options.inSampleSize = である;
ビットマップ bmp = null;
試す {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 戻り缩略図
} catch (OutOfMemoryError e) {
// TODO: 例外を処理します
System.gc();
bmp = null;
}
bmp を返します。
}