title: How to upload images with text parameters to server using Android Studio
body: <p>I am a newbie to Android. Recently, I am developing an application that upload image with some text using Android Studio. </p>
  
  <p>I have facing some problems when developing the application. I can upload the image the server. However, I facing a problem uploading the text with the image. I really no idea where goes wrong. I hope I can get some guidelines from here and you are free to change the code. </p>
  
  <p>Your help is really appreciated. 
  <a href="https://i.stack.imgur.com/rOzjT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rOzjT.jpg" alt="Here is the screenshot of the application"></a></p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>private void uploadVideo(final String name) {
  
      class UploadVideo extends AsyncTask&lt;Void, Void, String&gt; {
  
          ProgressDialog uploading;
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              uploading = ProgressDialog.show(MainActivityUpload.this, "Uploading File", "Please wait...", false, false);
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              uploading.dismiss();
              textViewResponse.setText(Html.fromHtml("&lt;b&gt;Uploaded at &lt;a href='" + s + "'&gt;" + s + "&lt;/a&gt;&lt;/b&gt;"));
              textViewResponse.setMovementMethod(LinkMovementMethod.getInstance());
          }
  
          @Override
          protected String doInBackground(Void... params) {
              upload u = new upload();
              String msg = u.uploadVideo(selectedPath , name); //the name is passed uploadVideo function in upload.java
              return msg;
          }
      }
      UploadVideo uv = new UploadVideo();
      uv.execute();
  }
  </code></pre>
  
  <p><strong>upload.java</strong></p>
  
  <pre><code>public class upload {
  
  public static final String UPLOAD_URL= "http://192.168.1.10/VideoUpload/upload.php";
  
  private int serverResponseCode;
  
  public String uploadVideo(String file , String name) {
  
      String fileName = file;
      HttpURLConnection conn = null;
      DataOutputStream dos = null;
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
  
      File sourceFile = new File(file);
      if (!sourceFile.isFile()) {
          Log.e("Huzza", "Source File Does not exist");
          return null;
      }
  
      try {
          FileInputStream fileInputStream = new FileInputStream(sourceFile);
          URL url = new URL(UPLOAD_URL);
          conn = (HttpURLConnection) url.openConnection();
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setUseCaches(false);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Connection", "Keep-Alive");
          conn.setRequestProperty("ENCTYPE", "multipart/form-data");
          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
          conn.setRequestProperty("myFile", fileName);
          conn.setRequestProperty("name", name); //the name of the image is passed here
          dos = new DataOutputStream(conn.getOutputStream());
  
          dos.writeBytes(twoHyphens + boundary + lineEnd);
          dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
          dos.writeBytes(lineEnd);
  
          bytesAvailable = fileInputStream.available();
          Log.i("Huzza", "Initial .available : " + bytesAvailable);
  
          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);
  
          dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
          dos.writeBytes(lineEnd);
          dos.writeBytes(name + lineEnd);
          dos.writeBytes(twoHyphens + boundary + lineEnd);
  
          serverResponseCode = conn.getResponseCode();
  
          fileInputStream.close();
          dos.flush();
          dos.close();
  
      } catch (MalformedURLException ex) {
          ex.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      if (serverResponseCode == 200) {
          StringBuilder sb = new StringBuilder();
          try {
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                      .getInputStream()));
              String line;
              while ((line = rd.readLine()) != null) {
                  sb.append(line);
              }
              rd.close();
          } catch (IOException ioex) {
          }
          return sb.toString();
      }else {
          return "Could not upload";
      }
  }
  }
  </code></pre>
  
  <p><strong>upload.php</strong></p>
  
  <pre><code>&lt;?php
  
   if($_SERVER['REQUEST_METHOD']=='POST'){
   $file_name = $_FILES['myFile']['name'];
   $file_size = $_FILES['myFile']['size'];
   $file_type = $_FILES['myFile']['type'];
   $temp_name = $_FILES['myFile']['tmp_name'];
   $name      = $_POST['name'];
  
   require_once('dbConnect.php');
  
          $sql ="SELECT id FROM uploads ORDER BY id ASC";
  
          $res = mysqli_query($con,$sql);
  
          $id = 0;
  
          while($row = mysqli_fetch_array($res)){
                  $id = $row['id'];
          }
  
   $location = "uploads/";
  
   $actualpath = "http://192.168.1.9/VideoUpload/$location$file_name";
  
   $sql = "INSERT INTO uploads (image,name) VALUES ('$actualpath','$name')";
  
       if(mysqli_query($con,$sql)){
                  move_uploaded_file($temp_name, $location.$file_name);
                  echo "Successfully Uploaded";
       }
       mysqli_close($con);
   }else{
   echo "Error";
   }
  ?&gt;
  </code></pre>