java根据url抓取并生成缩略图的示例
java根据url抓取并生成缩略图
publicstaticBitmaploadImageFromUrl(Stringurl,intsc){
URLm;
InputStreami=null;
BufferedInputStreambis=null;
ByteArrayOutputStreamout=null;
byteisBuffer[]=newbyte[1024];
if(url==null)
returnnull;
try{
m=newURL(url);
i=(InputStream)m.getContent();
bis=newBufferedInputStream(i,1024*4);
out=newByteArrayOutputStream();
intlen=0;
while((len=bis.read(isBuffer))!=-1){
out.write(isBuffer,0,len);
}
out.close();
bis.close();
}catch(MalformedURLExceptione1){
e1.printStackTrace();
returnnull;
}catch(IOExceptione){
e.printStackTrace();
}
if(out==null)
returnnull;
byte[]data=out.toByteArray();
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeByteArray(data,0,data.length,options);
options.inJustDecodeBounds=false;
intbe=(int)(options.outHeight/(float)sc);
if(be<=0){
be=1;
}elseif(be>3){
be=3;
}
options.inSampleSize=be;
Bitmapbmp=null;
try{
bmp=BitmapFactory.decodeByteArray(data,0,data.length,options);//返回缩略图
}catch(OutOfMemoryErrore){
//TODO:handleexception
System.gc();
bmp=null;
}
returnbmp;
}
相关文章
- java bufferedwriter 编码,Java BufferedWriter对象与utf-8
- java backoff_Java BackOff类代码示例
- 手机java程序_2020年最流行的Java开发技术
- java启动器_JAVA基础:Java 启动器如何查找类
- Java是什么?Java到底能干嘛?
- java session id 生成_Java通过sessionId获取Session「建议收藏」
- java setproperty 未生效_Java System类setProperty()方法及示例[通俗易懂]
- Java递归详解_java难不难学
- Java异常&处理异常
- JAVA生成uuid_java接口default方法
- Java开发之导出excel工具类详解编程语言
- 服务Linux启动Java服务:一步步去完成(linux启动java)
- 玩转Linux:Java开发入门指南(linux上开发java)
- Linux下安装Java 开发环境指南(linux装java环境)
- Oracle与Java的结合图像处理的快速开发(java图片oracle)
- java线程并发countdownlatch类使用示例
- java随机字符串生成示例
- 使用java将动态网页生成静态网页示例
- java用户名密码验证示例代码分享
- java生成随机数(字符串)示例分享
- java实现计算周期性提醒的示例
- java查询mongodb中的objectid示例
- java实现文件读写与压缩实例