Question

In an application, I am capturing video and converting the video in byte array. The byte Array is uploaded on server.

The length of the byte array on Server does not match up with the length of the Array that I had sent from device using web services.

The video on server end when decoded is corrupted and does not play up.

I am using below code for sending the Video in stream on server.

At Android End by using POST call:

public String callRestfullWebService(String url, ArrayList<NameValuePair> listParamsWithValues, byte[] parameter,
            int serviceFormat, boolean isPostRequest) throws Exception {
        String responseString = "";
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT_MILISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost postRequest = null;
        HttpGet getRequest = null;
        if(isPostRequest){
            postRequest = new HttpPost(url);
        }else{
            getRequest = new HttpGet(url);
        }
        switch (serviceFormat) {
        case GENERAL_RESTFUL_CALLING:
            break;
        case URL_ENCODED_FORM_ENTITY:
            postRequest.setEntity(new UrlEncodedFormEntity(listParamsWithValues));
            break;
        case BYTE_ENTITY_FORM:
            ByteArrayEntity byteArrayEntity = new ByteArrayEntity(parameter);
            postRequest.setEntity(byteArrayEntity);
            break;
            /*case STRING_ENTITY_FORM:
            StringEntity stringEntity=new StringEntity(parameter);
            postRequest.setEntity(stringEntity);
            break;*/
        }
        HttpResponse response = null;;
        if(isPostRequest){
            response = client.execute(postRequest);
        }else{
            response = client.execute(getRequest);
        }
        responseString = request(response); // here you get the response
        System.out.println(responseString); // this line will print the
        return responseString;
    }

And at server end with .NET services:

 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "VideoTest", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public Stream VideoTest(Stream fileContents)
    {
    try
    {
    // int length = 1024 * 4096;
    //int bytesRead = 0;
    Byte[] buffer = StreamToByteArray(fileContents);
    var newArray = new byte[buffer.Length-1];

    string path = HttpContext.Current.Server.MapPath("~/Weddingimage/" + Guid.NewGuid().ToString() + ".mp4");

    //fileContents.Write(newArray, 1, buffer.Length);
    // Array.Copy(buffer, 0, newArray, 1, buffer.Length);
    FileStream fileStream = new FileStream(path, FileMode.Create);
    for (int i = 1; i < buffer.Length; i++)
    {
    fileStream.WriteByte(buffer[i]);
    }

    //Byte[] buffer1 = StreamToByteArray(fileStream);
    //File.Create(path, buffer1.Length);
    //File.WriteAllBytes(path, buffer1);

    //int length = 1024 * 4096;
    //int bytesRead = 0;
    //Byte[] buffer = new Byte[length];
    //using (FileStream fileStream = new FileStream(path, FileMode.Create))
    //{
    // do
    // {
    // bytesRead = fileContents.Read(buffer, 0, length);
    // fileStream.Write(buffer, 0, bytesRead);
    // }
    // while (bytesRead > 0);
    //}

    string lines = "\n FileLength :" + Convert.ToString(buffer.Length) + "\r UploadTime :" + DateTime.Now + "\r";
    if (!File.Exists(HttpContext.Current.Server.MapPath("~/VideoLog.txt")))
    {
    System.IO.StreamWriter file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath("~/VideoLog.txt"));
    file.WriteLine(lines);
    file.Close();
    }
    else
    {
    StreamWriter oFile = File.AppendText(HttpContext.Current.Server.MapPath("~/VideoLog.txt"));
    // System.IO.StreamWriter file = new System.IO.StreamWriter();
    oFile.WriteLine(lines);
    oFile.Close();
    }

    return new MemoryStream(Encoding.UTF8.GetBytes(ConvertMe.ToJson(ConvertTOjsonString("Video Uploaded."))));
    }
    catch (Exception ex)
    {
    CommonClass.SaveRX_Log("VideoFrame", "1", ex.Source, ex.Message, ex.StackTrace);
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    return new MemoryStream(Encoding.UTF8.GetBytes(ConvertMe.ToJson(ConvertTOjsonString(ex.Message))));
    }
    }

Please suggest.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top