2013-11-10 27 views
1

我開發了我自己的rest api,使用單聲道的c#servicestack。除了文件下載以外,它的工作方式與預期相同。我注意到它在文件的開頭附加了一些位。例如看到下面的圖片:this hexa representation of a downloaded image什麼是覆蓋com.google.Volley上的圖像解析的最佳方式?

我向單聲道bugzilla提交了bug,同時,我想覆蓋我的客戶端上的圖像響應,以刪除第一個附加的東西,使圖像工作。我在c#客戶端上通過編輯接收到的流,然後將其保存到文件,它工作正常。

我需要知道如何以及在何處我可以重寫凌空圖庫以獲得乾淨的圖像而不是畸形的圖像,並獲得最佳性能。

更新04:37下午: 我相信我需要修改com.android.volley.toolbox.ImageRequest >>我會嘗試它併發布解決方案,如果它與我一起工作。

問候, 沙欣

+0

額外的字節可能是因爲使用分塊編碼返回響應。在響應中是否存在「Transfer-Encoding:chunked」標頭?請參閱[本文](http://stackoverflow.com/q/17755322/795339)和[本文](http://stackoverflow.com/q/17757875/795339)瞭解更多信息。 –

+0

謝謝埃斯克,是的,這是同樣的問題。我會嘗試xamiran錯誤解決方案。 – Shaheen

回答

1

我修改方法doParse在com.android.volley.toolbox.ImageRequest

private Response<Bitmap> doParse(NetworkResponse response){ 

     byte[] data = response.data; 
     byte [] pattern = fromHexString("FFD8FFE000"); 
     int position = matchPosition(data,pattern); 
     if(position>0) 
      data = Arrays.copyOfRange(data, position, data.length-position); 
     .... 
     .... 
     .... 
     .... 
     ..} 

這裏是輔助方法予使用:

public static int matchPosition(byte [] a, byte [] b){ 
     int matchLength=0; 
     int maxSearch = 30>a.length?a.length:30; 
    for (int i =0;i<maxSearch;i++) { 
     if (a[i]==b[0] && i+b.length<a.length){ 
      for(int j = 0;j< b.length;j++) 
      { 
       if((i+j)==a.length-1) 
        return -1; 
       if(a[i+j]==b[j]) 
        matchLength++; 
      } 
      if(matchLength == b.length) 
       return i; 
      else 
       matchLength = 0; 
     } 
    } 
    return -1; 
    } 


private static byte[] fromHexString(final String encoded) { 
    if ((encoded.length() % 2) != 0) 
     throw new IllegalArgumentException("Input string must contain an even number of characters"); 

    final byte result[] = new byte[encoded.length()/2]; 
    final char enc[] = encoded.toCharArray(); 
    for (int i = 0; i < enc.length; i += 2) { 
     StringBuilder curr = new StringBuilder(2); 
     curr.append(enc[i]).append(enc[i + 1]); 
     result[i/2] = (byte) Integer.parseInt(curr.toString(), 16); 
    } 
    return result; 
} 

和這項工作解決了上面解釋的問題!

相關問題