`
flycomos.lee
  • 浏览: 276846 次
  • 性别: 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);

相关推荐

Global site tag (gtag.js) - Google Analytics