title: How to convert File to Byte array in Android that can be consumed in ASP.Net (SOAP) Web Service?
body: <p>i have a simple file upload web service in ASP.net (SOAP), here's is the code :</p>
  
  <pre><code>&lt;WebMethod()&gt; _
      Public Function UploadFile(ByVal f As Byte(), ByVal fileName As String) As String
          Dim result As String = Json.JsonConvert.SerializeObject(New Result("INFO"))
          Dim ms As MemoryStream
          Dim fs As FileStream
  
          Try
              ms = New MemoryStream(f)
              fs = New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Upload/") &amp; fileName, FileMode.Create)
  
              ms.WriteTo(fs)
  
              result = Json.JsonConvert.SerializeObject(New Result("Upload Successful", ""))
          Catch ex As Exception
              result = Json.JsonConvert.SerializeObject(New Result("Upload Failed", ex.Message))
              MyCustomClass.Utilities.EventsLog.CreateEventsLog("FileDALC.asmx - UploadFile", ex.Message, ex.Source, "Services", Utilities.EventsLogType.Failed)
          Finally
              If Not IsNothing(ms) Then
                  ms.Close()
                  ms.Dispose()
              End If 
  
              If Not IsNothing(fs) Then
                  fs.Close()
                  fs.Dispose()
              End If
          End Try 
  
          Return Result
      End Function
  </code></pre>
  
  <p>All i want is to call that web service from my Android device and send some byte array parameter, but i always failed to produce byte array that can be recognized by web service..</p>
  
  <p>Here's my code in Android device:</p>
  
  <pre><code>public class WebServiceUploadData {
      private static String NAMESPACE = "http://services.chameleonmobile.app/"; 
      private static String URL = "http://10.41.25.49/SERVICES/Chameleon.WS.TitaniumMobile/FileDALC.asmx";
      private static String SOAP_ACTION = "http://services.chameleonmobile.app/";
  
      public static String invokeUploadFile(byte[] _file, String _fileName, String webMethName) {
          String resTxt = "";
          SoapObject request = new SoapObject(NAMESPACE, webMethName);
  
          PropertyInfo file = new PropertyInfo();
          file.setName("f");
          file.setValue(_file);
  
          PropertyInfo fileName = new PropertyInfo();
          fileName.setName("fileName");
          fileName.setValue(_fileName);
          fileName.setType(String.class);
  
          request.addProperty(file);
          request.addProperty(fileName);
  
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
          envelope.dotNet = true;
          envelope.setOutputSoapObject(request);
          new MarshalBase64().register(envelope);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  
          try {
              androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
              SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
              resTxt = response.toString();
          }
          catch (Exception e) {
              e.printStackTrace();
  
              if (e.getMessage().contains("EHOSTUNREACH")){
                   resTxt += "Error occured (Method Not Found, Wrong Network) - " + e.getMessage();
              }
              else if(e.getMessage().contains("ENETUNREACH")){
                  resTxt += "Error occured (Network Offline and Unreachable) - " + e.getMessage();
              }
              else{
                  resTxt += "Error occured - " + e.getMessage();
              }   
          } 
  
          return resTxt;
      }
  }
  </code></pre>
  
  <p>And call that function in MainActivity.java asynchronously..</p>
  
  <p>i produce the byte array like this (uploadFile is byte array parameter that will be send to web service):</p>
  
  <pre><code>File dirFile = new File(Environment.getExternalStorageDirectory() + "/TempDownloadFile", "Halo.txt");
  FileInputStream fis = new FileInputStream(dirFile);
  
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  byte[] buf = new byte[1024];
  for (int readNum; (readNum = fis.read(buf)) != -1;) {
      bos.write(buf, 0, readNum);
  }                      
  byte[] bytes = bos.toByteArray();
  uploadFile = Base64.encode(bytes, Base64.DEFAULT);
  </code></pre>
  
  <p>and also try this :</p>
  
  <pre><code>byte[] uploadData = FileUtils.readFileToByteArray(dirFile);//Convert any file, image or video into byte array
  uploadFile = Base64.encode(uploadData, Base64.DEFAULT);//Base64.encodeToString(data, Base64.NO_WRAP);
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>uploadFile = Base64.encode(IOUtils.toByteArray(fis), Base64.DEFAULT);
  </code></pre>
  
  <p>But unfortunately, all methods that i've tried is not working for me and my soap web service still can't recoqnize my byte array..</p>
  
  <p>Does anyone know how to fix this? Thanks..</p>