关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法

关于BitmapFactory.decodeStream(is)方法无法正常解码为Bitmap对象的解决方法

分类: Android开发技术 2237人阅读 评论 (3) 收藏 举报
[java] view plain copy
  1.   

在android sdk 1.6版本API帮助文档中,其中关于BitmapFactory.decodeFactory.decodeStream(InputStream is)的帮助文档是这么说明的:

[html] view plain copy
  1.    
  2. Bitmap android.graphics.BitmapFactory.decodeStream(InputStream is)  
  3.   
  4. public static Bitmap decodeStream (InputStream is)   
  5. Since: API Level 1   
  6. Decode an input stream into a bitmap. < strong > If the input stream is null, or cannot be used to decode a bitmap, the function returns null </ strong > . The stream's position will be where ever it was after the encoded data was read.  
  7.   
  8. Parameters  
  9. is  The input stream that holds the raw data to be decoded into a bitmap.   
  10.   
  11. Returns  
  12. The decoded bitmap, or null if the image data could not be decoded.   

注意黑体字。以下是具体代码:

[java] view plain copy
  1. public   static  Bitmap bmpFromURL(URL imageURL){  
  2.   
  3.     Bitmap result = null ;  
  4.   
  5.     try  {  
  6.   
  7.         HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();  
  8.   
  9.         connection.setDoInput(true );  
  10.   
  11.         connection.connect();  
  12.   
  13.         InputStream input = connection.getInputStream();  
  14.   
  15.         result = BitmapFactory.decodeStream(input);  
  16.   
  17.   
  18.     } catch  (IOException e) {  
  19.   
  20.   
  21.         e.printStackTrace();  
  22.   
  23.     }  
  24.   
  25.     return  result;  
  26.   
  27. }  

后来调试发现,result为null,加之查看帮助文档中的黑体字,
所以在所获得的InputStream不为空的情况下,调用BitmapFactory.decodeStream(is)方法,他也有可能无法解码成 bitmap,刚开始我怀疑是本身图片地址有问题,或图片自身格式不正确,但通过浏览器查看,图片显示正常,而且,我是保存了几十张图片,但每次都会有个 别几张图片无法正常显示,需要重复下载三四次,才可能保存成功。

后来在一篇文章中才发现,原来这是android 1.6版本的一个bug!

有牛人提出的一个解决办法,我试了试,问题解决了

首先在原方法中改一句:

[java] view plain copy
  1. result = BitmapFactory.decodeStream( new  PatchInputStream(input));  


再创建一个类:

扫描二维码关注公众号,回复: 841125 查看本文章
[java] view plain copy
  1. public   class  PatchInputStream  extends  FilterInputStream{  
  2.   
  3.         protected  PatchInputStream(InputStream in) {  
  4.             super (in);  
  5.             // TODO Auto-generated constructor stub   
  6.         }  
  7.           
  8.         public   long  skip( long  n) throws  IOException{  
  9.             long  m=0l;  
  10.             while (m<n){  
  11.                 long  _m=in.skip(n-m);  
  12.                 if (_m==0l){  
  13.                     break ;  
  14.                 }  
  15.                 m+=_m;  
  16.             }  
  17.             return  m;  
  18.         }  
  19.           
  20.     }  

第二种方法:最终用的是这种方法

[java] view plain copy
  1. InputStream is = httpConn.getInputStream();  
[java] view plain copy
  1. if  (is ==  null ){  
  2.     throw   new  RuntimeException( "stream is null" );  
  3. }else {  
  4.     try  {  
  5.         byte [] data=readStream(is);  
  6.         if (data!= null ){  
  7.             bitmap = BitmapFactory.decodeByteArray(data, 0 , data.length);  
  8.         }  
  9.     } catch  (Exception e) {  
  10.         e.printStackTrace();  
  11.     }  
  12.       
  13.     is.close();  
  14. }  


[java] view plain copy
  1. /*  
  2.      * 得到图片字节流 数组大小  
  3.      * */   
  4.     public   static   byte [] readStream(InputStream inStream)  throws  Exception{        
  5.         ByteArrayOutputStream outStream = new  ByteArrayOutputStream();        
  6.         byte [] buffer =  new   byte [ 1024 ];        
  7.         int  len =  0 ;        
  8.         while ( (len=inStream.read(buffer)) != - 1 ){        
  9.             outStream.write(buffer, 0 , len);        
  10.         }        
  11.         outStream.close();        
  12.         inStream.close();        
  13.         return  outStream.toByteArray();        
  14.     } 

猜你喜欢

转载自endual.iteye.com/blog/1629549