title: Upload Image HTTP POST Android
body: <p>I'm newbie in android and having problem uploading image in android. I'd already tried to find examples but got no luck:</p>
  
  <p>I am having error saying: </p>
  
  <blockquote>
    <p><img src="https://i.stack.imgur.com/2vtTF.png" alt="enter image description here"></p>
  </blockquote>
  
  <p>and here is my code:
  I have Array named <code>"images"</code> which contains the paths of images that I want to upload
  e.g. /storage/sdcard0/DCIM/Camera/IMG_20131214_171438.jpg</p>
  
  <pre><code>//ONCLICK LISTENER    
  public OnClickListener upload_image = new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  if(images.size() != 0) {
  
                      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                      StrictMode.setThreadPolicy(policy);
  
                      for(int i=0; i&lt;images.size(); i++){
                          doFileUpload(images.get(i));
                      }
                  }
              }
          };
  </code></pre>
  
  <p>//UPLOAD</p>
  
  <pre><code>private void doFileUpload(String upload_file){
                  Log.d("Currently queued", upload_file);
                  HttpURLConnection conn = null;
                  DataOutputStream dos = null;
                  DataInputStream inStream = null;
                  String exsistingFileName = upload_file;
                  // Is this the place are you doing something wrong.
                  String lineEnd = "\r\n";
                  String twoHyphens = "--";
                  String boundary =  "acebdf13572468";
                  int bytesRead, bytesAvailable, bufferSize;
                  byte[] buffer;
                  int maxBufferSize = 1*1024*1024;
                  String urlString = "http://test.xponlm.com/MobileServices/api/ShipmentImages";
                  try
                  {
                      Log.e("MediaPlayer","Inside second Method");
                      FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
                      URL url = new URL(urlString);
                      conn = (HttpURLConnection) url.openConnection();
                      conn.setDoInput(true);
                      // Allow Outputs
                      conn.setDoOutput(true);
                      // Don't use a cached copy.
                      conn.setUseCaches(false);
                      // Use a post method.
                      conn.setRequestMethod("POST");
                      conn.setRequestProperty("Connection", "Keep-Alive");
                      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                      dos = new DataOutputStream( conn.getOutputStream() );
                      dos.writeBytes(twoHyphens + boundary + lineEnd);
                      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
                      dos.writeBytes(lineEnd);
  
                      dos.writeBytes(twoHyphens + boundary + lineEnd);
                      dos.writeBytes("Content-Disposition: form-data; name=\"ID\""+ lineEnd);
                      dos.writeBytes(lineEnd);
                      dos.writeBytes(strshipmentID + lineEnd);
  
                      dos.writeBytes(twoHyphens + boundary + lineEnd);
                      dos.writeBytes("Content-Disposition: form-data; name=\"ImageType\""+ lineEnd);
                      dos.writeBytes(lineEnd);
                      dos.writeBytes("JPG" + lineEnd);
  
                      dos.writeBytes(twoHyphens + boundary + lineEnd);
                      dos.writeBytes("Content-Disposition: form-data; name=\"FileType\""+ lineEnd);
                      dos.writeBytes(lineEnd);
                      dos.writeBytes(strtype + lineEnd);
  
                      Log.e("MediaPlayer","Headers are written");
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      buffer = new byte[bufferSize];
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
                      while (bytesRead &gt; 0) {
                          dos.write(buffer, 0, bufferSize);
                          bytesAvailable = fileInputStream.available();
                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                      }
  
                      dos.writeBytes(lineEnd);
                      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      String inputLine;
                      while ((inputLine = in.readLine()) != null)
  //                      tv.append(inputLine);
                          Log.d("inputLine", inputLine);
                      // close streams
                      Log.e("MediaPlayer","File is written");
                      fileInputStream.close();
                      dos.flush();
                      dos.close();
                  }
                  catch (MalformedURLException ex)
                  {
                      Log.e("MediaPlayer", "error1: " + ex.getMessage(), ex);
                  }
                  catch (IOException ioe)
                  {
                      Log.e("MediaPlayer", "error2: " + ioe.getMessage(), ioe);
                  }
  
                  //------------------ read the SERVER RESPONSE
                  try {
                      inStream = new DataInputStream ( conn.getInputStream() );
                      String str;            
  
                      while (( str = inStream.readLine()) != null) {
                          Log.e("MediaPlayer","Server Response"+str);
                      }
                      /*while((str = inStream.readLine()) !=null ){
  
                      }*/
                      inStream.close();
                  }
                  catch (IOException ioex){
                      Log.e("MediaPlayer", "error3: " + ioex.getMessage(), ioex);
                  }
              }
  </code></pre>
  
  <p>Did I missed somthing? Can you tell what is the problem on my code?</p>