2011-06-23 87 views
1

我使用下面的代碼從URL下載一個位圖,:圖像有時返回null圖像

 

myFileUrl = new URL(fileUrl); 


HttpURLConnection conn = (HttpURLConnection) myFileUrl 
        .openConnection(); 
conn.setDoInput(true); 
conn.connect(); 
InputStream is = conn.getInputStream(); 
Bitmap bmpTemp = BitmapFactory.decodeStream(is); 

但有時,我在一百倍意味着一次,位圖是空!任何機構知道什麼可能是問題,

謝謝

+0

這裏有好的方法 [此處輸入鏈路描述] [1] [1] :http://stackoverflow.com/questions/8085916/android-download-of-bitmap-returns-null-sometimes – baozi

回答

1

有一個在連接中的錯誤。它有一個修復程序,它使用FlushedInputStream裝飾器功能。 這裏是它的代碼:

/* 
    * An InputStream that skips the exact number of bytes provided, unless it reaches EOF. 
    */ 
    public static class FlushedInputStream extends FilterInputStream { 

     public FlushedInputStream(InputStream inputStream) { 
      super(inputStream); 
     } 
     @Override 
     public long skip(long n) throws IOException { 
      long totalBytesSkipped = 0L; 
      while (totalBytesSkipped < n) { 
       long bytesSkipped = in.skip(n - totalBytesSkipped); 
       if (bytesSkipped == 0L) { 
        int b = read(); 
        if (b < 0) { 
         break; // we reached EOF 
        } else { 
         bytesSkipped = 1; // we read one byte 
        } 
       } 
       totalBytesSkipped += bytesSkipped; 
      } 
      return totalBytesSkipped; 
     } 
    } 

的用法是:

BitmapFactory.decodeStream(new FlushedInputStream(is)); 
+0

非常感謝答案!你是否有官方文檔發送給我們的客戶,他不會確信這是一個android bug,直到他看到官方文檔,再次感謝 –

+0

http://code.google.com/p/android/issues/detail?id = 6066 – DArkO

+0

Thanksss!這是很多的幫助 –

相關問題