`
flycomos.lee
  • 浏览: 282542 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决)android中将网络图片转化为缩略图

 
阅读更多

[转]java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法

最近在做电信的一个视频地图项目时,需要获取网络图片预览,用到图片缩略图技术,通过参考了很多同行的方法,本人写了以下获取网络图片缩略图的代码,如有不妥,望高手指正,谢谢。以下是实现方法:

 

 

获取缩略图关键代码

 

 

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);                          //获取真正的图片对象(缩略图)

 

 

 

 

 

 

以下是批量获取网络图片缩略图的详细代码:

 

 

Android代码  收藏代码
  1. /**  
  2.      * 根据图片网络地址获取图片的byte[]类型数据  
  3.      * @param urlPath 图片网络地址  
  4.      * @return 图片数据  
  5.      */  
  6.     public byte[] getImageFromURL(String urlPath){  
  7.         byte[] data=null;  
  8.         InputStream is=null;  
  9.         HttpURLConnection conn=null;  
  10.         try {  
  11.             URL url=new URL(urlPath);  
  12.             conn=(HttpURLConnection) url.openConnection();  
  13.             conn.setDoInput(true);  
  14.             //conn.setDoOutput(true);  
  15.             conn.setRequestMethod("GET" );  
  16.             conn.setConnectTimeout(6000 );  
  17.             is=conn.getInputStream();  
  18.             if(conn.getResponseCode()==200 ){  
  19.                 data=readInputStream(is);  
  20.             }  
  21.             else  System.out.println("发生异常!" );  
  22.               
  23.         } catch (MalformedURLException e) {  
  24.             e.printStackTrace();  
  25.         } catch (IOException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.         finally{  
  29.             conn.disconnect();  
  30.             try {  
  31.                 is.close();  
  32.             } catch (IOException e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.         return data;  
  37.     }  
  38.       
  39.     /**  
  40.      * 读取InputStream数据,转为byte[]数据类型  
  41.      * @param is  InputStream数据  
  42.      * @return  返回byte[]数据  
  43.      */  
  44.     public byte[] readInputStream(InputStream is) {  
  45.         ByteArrayOutputStream baos=new ByteArrayOutputStream();  
  46.         byte[] buffer=new byte[1024 ];  
  47.         int length=-1 ;  
  48.         try {  
  49.             while((length=is.read(buffer))!=-1 ){  
  50.                 baos.write(buffer, 0 , length);  
  51.             }  
  52.             baos.flush();  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         byte[] data=baos.toByteArray();  
  57.         try {  
  58.             is.close();  
  59.             baos.close();  
  60.         } catch (IOException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.         return data;  
  64.     }  
  65.       
  66.     /**  
  67.      * 根据网络图片地址集批量获取网络图片  
  68.      * @param urlPath  网络图片地址数组  
  69.      * @return    返回Bitmap数据类型的数组  
  70.      */  
  71.     public Bitmap[] getBitmapArray(String[] urlPath){  
  72.         int length=urlPath.length;  
  73.         if(urlPath==null||length<1 ){  
  74.             return null;  
  75.         }  
  76.         else{  
  77.             Bitmap[] bitmaps=new Bitmap[length];  
  78.             for (int i = 0 ; i < length; i++) {  
  79.                 byte[] imageByte=getImageFromURL(urlPath[i].trim());  
  80.                   
  81.                 //以下是把图片转化为缩略图再加载  
  82.                 BitmapFactory.Options options = new BitmapFactory.Options();   
  83.                 options.inJustDecodeBounds = true;  
  84.                 Bitmap bitmap=BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length, options);  
  85.                 options.inJustDecodeBounds = false;  
  86.                 int be = (int)(options.outHeight / (float)200 );   
  87.                 if (be <= 0 )   be =  1 ;   
  88.                 options.inSampleSize = be;   
  89.                 bitmaps[i]=BitmapFactory.decodeByteArray(imageByte, 0 , imageByte.length,options);  
  90.             }  
  91.             return bitmaps;  
  92.         }  
  93.           
  94.     } 
分享到:
评论
1 楼 yahier 2012-03-16  
我也是 Bitmap myBitmap = BitmapFactory.decodeFile(path);

相关推荐

    android_内存溢出处理

    如果图片的大小超过了 RAM 的内存,就会出现 java.lang.OutOfMemoryError:bitmap size exceeds VM budget 异常。解决这个问题可以使用 BitmapFactory.Options 对象,通过设置 inSampleSize 属性来缩放图片的大小。 ...

    处理bitmap内存溢出问题

    当应用程序尝试加载或操作一张超出虚拟机内存预算的`Bitmap`时,系统会抛出`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常,导致应用崩溃。为了解决这个问题,开发者需要采取一些策略来优化图片...

    android处理图片内存溢出VM.pdf

    当应用尝试加载超出虚拟机内存预算的大图片时,会抛出一个常见的异常java.lang.OutOfMemoryError: bitmap size exceeds VM budget。图片加载时的内存溢出主要因为大尺寸图片占用的内存远远超过了分配给应用的内存...

    android 处理图片内存溢出 VM.pdf

    当尝试加载大尺寸的图片时,可能会遇到`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`错误,这是因为Android虚拟机(VM)的内存预算有限,尤其是对于较大的图片,如果直接加载原图,会消耗大量内存,...

    Android’s 24 MB memory limit

    E/AndroidRuntime(12517): java.lang.OutOfMemoryError: bitmap size exceeds VM budget ``` 这段日志显示了当尝试分配1MB内存时,由于超出了24MB的限制,导致了应用崩溃。 #### 三、解决方案 针对这个问题,...

    ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf

    当Android系统尝试分配一块超过其当前可用内存大小的内存时,会抛出`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常。从日志可以看出,问题出现在尝试解码一个资源(可能是图片)到Bitmap对象时,...

    解析activity之间数据传递方法的详解

    频繁使用静态的Bitmap或Drawable可能导致`java.lang.OutOfMemoryError: bitmap size exceeds VM budget`异常,因此应谨慎使用。 第三种方法是基于外部存储的数据传输,包括File、SharedPreferences、SQLite和...

Global site tag (gtag.js) - Google Analytics