java根據(jù)url抓取并生成縮略圖的示例
java根據(jù)url抓取并生成縮略圖
public static Bitmap loadImageFromUrl(String url, int sc) {
URL m;
InputStream i = null;
BufferedInputStream bis = null;
ByteArrayOutputStream out = null;
byte isBuffer[] = new byte[1024];
if (url == null)
return null;
try {
m = new 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();
return null;
} catch (IOException e) {
e.printStackTrace();
}
if (out == null)
return null;
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 (be <= 0) {
be = 1;
} else if (be > 3) {
be = 3;
}
options.inSampleSize = be;
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回縮略圖
} catch (OutOfMemoryError e) {
// TODO: handle exception
System.gc();
bmp = null;
}
return bmp;
}
相關(guān)文章
springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決
這篇文章主要介紹了springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Spring Boot與RabbitMQ結(jié)合實現(xiàn)延遲隊列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實現(xiàn)延遲隊列的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11修改Maven settings.xml 后配置未生效的解決
這篇文章主要介紹了修改Maven settings.xml 后配置未生效的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10