2022年批量获取网络图片

上传人:壹****1 文档编号:567384185 上传时间:2024-07-20 格式:PDF 页数:6 大小:126.54KB
返回 下载 相关 举报
2022年批量获取网络图片_第1页
第1页 / 共6页
2022年批量获取网络图片_第2页
第2页 / 共6页
2022年批量获取网络图片_第3页
第3页 / 共6页
2022年批量获取网络图片_第4页
第4页 / 共6页
2022年批量获取网络图片_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《2022年批量获取网络图片》由会员分享,可在线阅读,更多相关《2022年批量获取网络图片(6页珍藏版)》请在金锄头文库上搜索。

1、java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法最近在做电信的一个视频地图项目时,需要获取网络图片预览,用到图片缩略图技术,通过参考了很多同行的方法,本人写了以下获取网络图片缩略图的代码,如有不妥, 望高手指正,谢谢。以下是实现方法:获取缩略图关键代码byte imageByte=getImageFromURL(urlPathi.trim(); /以下是把图片转化为缩略图再加载BitmapFactory.Options options = new BitmapFactory.Options(); options.inJu

2、stDecodeBounds = true; /首先设置 .inJustDecodeBounds为 true Bitmap bitmap=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length, options); /这时获取到的bitmap 是 null 的,尚未调用系统内存资源options.inJustDecodeBounds = false; 得到图片有宽和高的options对象后,设置.inJustDecodeBounds为 false 。int be = (int)(options.outHeight / (floa

3、t)200); if (be = 0) be = 1; options.inSampleSize = be; /计算得到图片缩小倍数bitmapsi=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); /获取真正的图片对象(缩略图)以下是批量获取网络图片缩略图的详细代码:Android 代码名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - /

4、* * 根据图片网络地址获取图片的byte 类型数据* param urlPath 图片网络地址* return 图片数据*/ public byte getImageFromURL(String urlPath) byte data=null; InputStream is=null; HttpURLConnection conn=null; try URL url=new URL(urlPath); conn=(HttpURLConnection) url.openConnection(); conn.setDoInput(true); /conn.setDoOutput(true); c

5、onn.setRequestMethod(GET); conn.setConnectTimeout(6000); is=conn.getInputStream(); if(conn.getResponseCode()=200) data=readInputStream(is); else System.out.println(发生异常! ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - catch (MalformedURL

6、Exception e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally conn.disconnect(); try is.close(); catch (IOException e) e.printStackTrace(); return data; /* * 读取 InputStream数据,转为byte 数据类型* param is InputStream数据* return 返回 byte 数据*/ public byte readInputStream(InputStream is) 名

7、师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte buffer=new byte1024; int length=-1; try while(length=is.read(buffer)!=-1) baos.write(buffer, 0, length); baos.flush(); catch (IOExce

8、ption e) e.printStackTrace(); byte data=baos.toByteArray(); try is.close(); baos.close(); catch (IOException e) e.printStackTrace(); return data; /* 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - * 根据网络图片地址集批量获取网络图片* param urlPath 网络图片地址数组

9、* return 返回 Bitmap 数据类型的数组*/ public Bitmap getBitmapArray(String urlPath) int length=urlPath.length; if(urlPath=null|length1) return null; else Bitmap bitmaps=new Bitmaplength; for (int i = 0; i length; i+) byte imageByte=getImageFromURL(urlPathi.trim(); /以下是把图片转化为缩略图再加载BitmapFactory.Options options

10、 = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length, options); options.inJustDecodeBounds = false; int be = (int)(options.outHeight / (float)200); if (be = 0) be = 1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - options.inSampleSize = be; bitmapsi=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); return bitmaps; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号