在com.google.Volley上覆盖图像解析的最佳方法是什么?

时间:2013-11-10 08:40:32

标签: java android rest mobile android-volley

我在mono上使用c#servicestack开发了自己的rest api。它可以按预期工作,除了文件下载。我注意到它在文件的开头附加了一些位。例如,请参见下图:this hexa representation of a downloaded image

我向mono bugzilla提交了bug,同时,我想在我的客户端上覆盖图像响应,删除第一个附加的东西以使图像工作。我在c#客户端上通过编辑收到的流然后将其保存到文件来尝试它并且工作正常。

我需要知道如何以及在哪里可以覆盖凌空库以获得干净的图像而不是具有最佳性能的畸形图像。

更新04:37 PM: 我相信我需要修改com.android.volley.toolbox.ImageRequest>>我会尝试并发布解决方案,如果它适用于我。

此致 沙欣

1 个答案:

答案 0 :(得分:1)

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

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;
}

这项工作解决了上面解释的问题!

相关问题