關於BitmapFactory.decodeStream(is)方法無法正常解碼爲Bitmap對象的解決方法

轉自:http://blog.csdn.net/andypan1314/article/details/6670320

[java] view plaincopy
  1.   
在android sdk 1.6版本API幫助文檔中,其中關於BitmapFactory.decodeFactory.decodeStream(InputStream is)的幫助文檔是這麼說明的:

[html] view plaincopy
  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 plaincopy
  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 plaincopy
  1. result = BitmapFactory.decodeStream(new PatchInputStream(input));  

再創建一個類:


[java] view plaincopy
  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 plaincopy
  1. InputStream is = httpConn.getInputStream();  

[java] view plaincopy
  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 plaincopy
  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.     }  


發佈了22 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章