title: Couldn't send the image to the web server database for android
body: <p>I was instructed to send a report in this format</p>
  
  <pre><code>Name=tan ah kow 
  
  Email=myemail@google.com
  
  Contact=81234567
  
  Block=123
  
  Street=Tampines Central
  
  Description=Dirty water puddle
  
  uploadedfile=a5fa23423-4907098 (BASE64-ENCODED STRING)
  
  index=1
  </code></pre>
  
  <p>Set the HTTP Header "Content-Type" as "multipart/form-data; boundary=BOUNDARY_STRING"</p>
  
  <p>Form the HTTP Body Message according to the HTTP POST Specifications for multipart/form-data
  FORMAT IS</p>
  
  <pre><code>--BOUNDARY_STRING\r\n
  Content-Disposition: form-data; name=\"Name\"\r\n\r\n
  tan ah kow\r\n
  --BOUNDARY_STRING\r\n
  Content-Disposition: form-data; name=\"Email\"\r\n\r\n
  myemail@google.com\r\n
  ...
  ...
  ...
  --BOUNDARY_STRING\r\n
  Content-Dispostion: form-data; name=\"uploadedfile\"; filename=\"android.png\"\r\n
  Content-Type: MIME-TYPE\r\n
  Content-Transfer-Encoding: binary\r\n\r\n
  a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098\r\n
  --BOUNDARY_STRING--
  </code></pre>
  
  <p>If there is only 1 photo uploaded, just send one http post, and set index=1.
  If there are 2 photos, send two http post of the same data (name, email, contact, etc) except for uploadedfile, which should be the data for the second photo, and set index=2.
  If there are 3 photos, send three http post of the same data (name, email, etc) except for uploadedfile, which should be the data of the third photo, and set index=3.</p>
  
  <p>The server will handle the linking of the photos to the correct report.</p>
  
  <p>Following code for send images to server but didn't work. I hope anyone can help me.</p>
  
  <pre><code>String urlFEEDBACK = "http://zendragon.sg/tampinestc/report.php";
  
       private class NewFeedbackReportAsyncTask extends AsyncTask&lt;HashMap&lt;String, String&gt;, Void, String&gt; {
  
          @Override
          protected void onPreExecute () {
  
          }
  
          @Override
          protected String doInBackground(HashMap&lt;String, String&gt;... arrHashMapParameters) {
              HashMap&lt;String, String&gt; hashmapInput = arrHashMapParameters[0];
              String strPost = "source=android";
              Iterator&lt;Entry&lt;String, String&gt;&gt; it = hashmapInput.entrySet().iterator();
              while (it.hasNext()) {
                  Map.Entry&lt;String, String&gt; pairs = (Map.Entry&lt;String, String&gt;)it.next();
                  strPost += "&amp;"+(pairs.getKey() + "=" + pairs.getValue());
                  it.remove();
              }
              String strFileData = convertPhotoToString(bmp);
              String result = null;
              try {
                  result = multipartRequest(urlFEEDBACK, strPost, strFileData);
              } catch (Exception e) {
                  Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
              }
              return result;
          }
  
          @Override
          protected void onPostExecute(String result) {
              if (progressDialog.isShowing()) {
                  progressDialog.dismiss();
              }
              showResults();
          }
  
      }
  </code></pre>
  
  <p><em><strong>This code is to convert bitmap to BASE64-ENCODED String</em></strong></p>
  
  <pre><code>protected String convertPhotoToString(Bitmap theBitMap) {
      if(theBitMap == null) {
          return null;
      }
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      theBitMap.compress(CompressFormat.JPEG, 60, bos); 
      byte[] array = bos.toByteArray();
  
      return android.util.Base64.encodeToString(array, android.util.Base64.NO_WRAP);
  } 
  </code></pre>
  
  <p><em><strong>This is the codes that send to the server.</em></strong></p>
  
  <pre><code> public String multipartRequest(String urlFEEDBACK2, String strPost, String strFileData) throws ParseException, IOException{
      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;
      InputStream inputStream = null;
      String twoHyphens = "--";
      String lineEnd = "\r\n";
      String boundary = "BOUNDARY_STRING";
      String result = "";
  
      try {
          URL url = new URL(urlFEEDBACK2);
          connection = (HttpURLConnection) url.openConnection();
          connection.setDoInput(true);
          connection.setDoOutput(true);
          connection.setUseCaches(false);
  
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Connection", "Keep-Alive");
          connection.setRequestProperty("User-Agent",
                  "Android Multipart HTTP Client 1.0");
          connection.setRequestProperty("Content-Type",
                  "multipart/form-data; boundary=" + boundary);
  
          outputStream = new DataOutputStream(connection.getOutputStream());
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Name\"" 
                  + lineEnd + lineEnd + strName + lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Email\"" 
                  + lineEnd + lineEnd + strEmail + lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Contact\"" 
                  + lineEnd + lineEnd + strPhoneNo + lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Block\"" 
                  + lineEnd + lineEnd + strBLK + lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Street\"" 
                  + lineEnd + lineEnd + strStreet + lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Description\"" 
                  + lineEnd + lineEnd + strDescription + lineEnd);
  
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"android.png\"" + lineEnd);
          outputStream.writeBytes("Content-Type: image/png" + lineEnd);
          outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
          outputStream.writeBytes(lineEnd);
  
          if(bmp!=null) {
                         //EDITED
             outputStream.writeBytes(strFileData);
             //ByteArrayOutputStream baos = new ByteArrayOutputStream();
             //bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
             //baos.writeTo(outputStream);
             //baos.close();
  
          }
  
          outputStream.writeBytes(lineEnd);
          outputStream.writeBytes(twoHyphens + boundary + twoHyphens);
  
          inputStream = connection.getInputStream(); 
          result = this.convertStreamToString(inputStream);
  
          // Clean up
          inputStream.close();
          outputStream.flush();
          outputStream.close();
  
          // Log see debug
          Log.d(getString(R.string.app_name), "Result: "+ result);
          checkResponse(result);
          return result;
  
      } catch (Exception e) {
          Log.e("MultipartRequest", "Multipart Form Upload Error");
          e.printStackTrace();
          return "error";
      }
  }
  </code></pre>
  
  <p>*<strong>EDITED: *</strong>
  I can't seem to see any problem with my codes to send/write the image to the web server. Hope anyone can point out what is my mistake here. I need to solve this asap as my boss keep on asking me. Hope anyone can help me out here. Any help will be appreciate. Thanks in advance.</p>