最近在做电信的一个视频地图项目时,需要获取网络图片预览,用到图片缩略图技术,通过参考了很多同行的方法,本人写了以下获取网络图片缩略图的代码,如有不妥,望高手指正,谢谢。以下是实现方法:
获取缩略图关键代码
byte[] imageByte=getImageFromURL(urlPath[i].trim());
//以下是把图片转化为缩略图再加载
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = 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 / (float)200);
if (be <= 0) be = 1;
options.inSampleSize = be; //计算得到图片缩小倍数
bitmaps[i]=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options); //获取真正的图片对象(缩略图)
以下是批量获取网络图片缩略图的详细代码:
/**
* 根据图片网络地址获取图片的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);
conn.setRequestMethod("GET");
conn.setConnectTimeout(6000);
is=conn.getInputStream();
if(conn.getResponseCode()==200){
data=readInputStream(is);
}
else System.out.println("发生异常!");
} catch (MalformedURLException 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) {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=-1;
try {
while((length=is.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data=baos.toByteArray();
try {
is.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
/**
* 根据网络图片地址集批量获取网络图片
* @param urlPath 网络图片地址数组
* @return 返回Bitmap数据类型的数组
*/
public Bitmap[] getBitmapArray(String[] urlPath){
int length=urlPath.length;
if(urlPath==null||length<1){
return null;
}
else{
Bitmap[] bitmaps=new Bitmap[length];
for (int i = 0; i < length; i++) {
byte[] imageByte=getImageFromURL(urlPath[i].trim());
//以下是把图片转化为缩略图再加载
BitmapFactory.Options options = 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;
options.inSampleSize = be;
bitmaps[i]=BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length,options);
}
return bitmaps;
}
}
参考文章:http://www.cnblogs.com/RayLee/archive/2010/11/09/1872856.html
分享到:
相关推荐
如果图片的大小超过了 RAM 的内存,就会出现 java.lang.OutOfMemoryError:bitmap size exceeds VM budget 异常。解决这个问题可以使用 BitmapFactory.Options 对象,通过设置 inSampleSize 属性来缩放图片的大小。 ...
当应用程序尝试加载或操作一张超出虚拟机内存预算的`Bitmap`时,系统会抛出`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常,导致应用崩溃。为了解决这个问题,开发者需要采取一些策略来优化图片...
当应用尝试加载超出虚拟机内存预算的大图片时,会抛出一个常见的异常java.lang.OutOfMemoryError: bitmap size exceeds VM budget。图片加载时的内存溢出主要因为大尺寸图片占用的内存远远超过了分配给应用的内存...
当尝试加载大尺寸的图片时,可能会遇到`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`错误,这是因为Android虚拟机(VM)的内存预算有限,尤其是对于较大的图片,如果直接加载原图,会消耗大量内存,...
E/AndroidRuntime(12517): java.lang.OutOfMemoryError: bitmap size exceeds VM budget ``` 这段日志显示了当尝试分配1MB内存时,由于超出了24MB的限制,导致了应用崩溃。 #### 三、解决方案 针对这个问题,...
当Android系统尝试分配一块超过其当前可用内存大小的内存时,会抛出`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常。从日志可以看出,问题出现在尝试解码一个资源(可能是图片)到Bitmap对象时,...
频繁使用静态的Bitmap或Drawable可能导致`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常,因此应谨慎使用。 第三种方法是基于外部存储的数据传输,包括File、SharedPreferences、SQLite和...