0

我正在编写一个必须在 wcf 网络服务上发送图片的 android 应用程序。我的应用程序可以联系网络服务并给它图片。但是,尺寸不同,我无法在网络服务上打开图片。

编辑:通过更改网络服务部分,我得到了完全相同的大小。但是,还是打不开。

Android部分(文件大小20ko):

File img;
try {
        Log.i("image", "get file");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        Log.i("call", "end build");

        MultipartEntity entity = new MultipartEntity();
        entity.addPart("data", new FileBody(f));

        httppost.setEntity(entity);
        Log.i("call", "call");
        HttpResponse response = httpclient.execute(httppost);
        Log.i("call", "After");

    }
catch (Exception e) {
    Log.i("error cal image", e.toString());
}

编辑:网络服务(文件大小 20ko):

[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "picture")]
public void UploadPicture(Stream image)
{
     var ms = new MemoryStream();
        image.CopyTo(ms);
        var streamBytes = ms.ToArray();
        FileStream f = new FileStream("C:\\appicture.jpg", FileMode.OpenOrCreate);
        f.Write(streamBytes, 0, streamBytes.Length);
        f.Close();
        ms.Close();
        image.Close();


}
4

1 回答 1

1

您以块的形式读取文件,但只写入最后一个块:

// following line is called once, should be called after each read
fileToupload.Write(bytearray, 0, bytearray.Length); 

所以试试这样:

/*...*/
do
{
    bytesRead = image.Read(bytearray, 0, bytearray.Length);
    totalBytesRead += bytesRead;
    fileToupload.Write(bytearray, 0, bytesRead);
} while (bytesRead > 0);

fileToupload.Close();
fileToupload.Dispose();
/*...*/
于 2012-06-26T13:57:27.753 回答