[Android Pro] 關於BitmapFactory.decodeStream(is)方法無法正常解碼為Bitmap對象的解決方法


在android sdk 1.6版本API幫助文檔中,其中關於BitmapFactory.decodeFactory.decodeStream(InputStream is)的幫助文檔是這么說明的:

 
Bitmap android.graphics.BitmapFactory.decodeStream(InputStream is)

public static Bitmap decodeStream (InputStream is) 
Since: API Level 1 
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.

Parameters
is  The input stream that holds the raw data to be decoded into a bitmap. 

Returns
The decoded bitmap, or null if the image data could not be decoded. 
public static Bitmap bmpFromURL(URL imageURL){
    Bitmap result = null;
    try {
        HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        result = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

后來調試發現,result為null,加之查看幫助文檔中的黑體字,
所以在所獲得的InputStream不為空的情況下,調用BitmapFactory.decodeStream(is)方法,他也有可能無法解碼成bitmap,剛開始我懷疑是本身圖片地址有問題,或圖片自身格式不正確,但通過瀏覽器查看,圖片顯示正常,而且,我是保存了幾十張圖片,但每次都會有個別幾張圖片無法正常顯示,需要重復下載三四次,才可能保存成功。

 

后來在一篇文章中才發現,原來這是android 1.6版本的一個bug!

 

有牛人提出的一個解決辦法,我試了試,問題解決了

首先在原方法中改一句:

result = BitmapFactory.decodeStream(new PatchInputStream(input));

再創建一個類:

public class PatchInputStream extends FilterInputStream{

        protected PatchInputStream(InputStream in) {
            super(in);
            // TODO Auto-generated constructor stub
        }
        
        public long skip(long n)throws IOException{
            long m=0l;
            while(m<n){
                long _m=in.skip(n-m);
                if(_m==0l){
                    break;
                }
                m+=_m;
            }
            return m;
        }
    }

第二種方法:最終用的是這種方法

InputStream is = httpConn.getInputStream();

  if (is == null){
       throw new RuntimeException("stream is null");
  }else{
       try {
           byte[] data=readStream(is);
           if(data!=null){
           bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
       }
  } catch (Exception e) {
       e.printStackTrace();
  }
   is.close();
  }

    /*
     * 得到圖片字節流 數組大小
     * */
    public static byte[] readStream(InputStream inStream) throws Exception{      
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();      
        byte[] buffer = new byte[1024];      
        int len = 0;      
        while( (len=inStream.read(buffer)) != -1){      
            outStream.write(buffer, 0, len);      
        }      
        outStream.close();      
        inStream.close();      
        return outStream.toByteArray();      
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM