title: upload image in android studio
body: <p>I'm trying to upload image to xampp server, with android-studio and php
  But I will not succeed
  Please help me</p>
  <p>here is my code(java and php):</p>
  <p>java code:
  in this codes im trying to select picture from gallery and show a dialog and then uploading image...</p>
  <pre><code>public final int REQUEST_OPEN_GALLERY = 5;
          ProgressDialog dialog;
          Thread uploadThread;
          String lineEnd = &quot;\r\n&quot;;
          String twoHyphens = &quot;--&quot;;
          String id = &quot;&quot;;
          String boundary = &quot;*****&quot;;
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024;
          public static Handler uploadHandler;
          public static String imageProfile = &quot;&quot;;
          
          profile_image.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  chooseFile();
              }
          });
  
      }
      private void chooseFile() {
          Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(intent, REQUEST_OPEN_GALLERY);
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == REQUEST_OPEN_GALLERY &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null) {
              Uri uri = data.getData();
              String[] info = {MediaStore.Images.Media.DATA};
              Cursor cursor = getActivity().getContentResolver().query(uri, info, null, null, null);
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(info[0]);
              final String filePath = cursor.getString(columnIndex);
              //Toast.makeText(MainActivity.this,filePath,Toast.LENGTH_SHORT).show();
              dialog = ProgressDialog.show(getContext(), &quot;upload&quot;, &quot;Uploading file...&quot;);
  
              uploadThread = new Thread(new Runnable() {
                  @Override
                  public void run() {
                      uploadFile(filePath);
                  }
              });
              uploadThread.start();
  
          }
      }
      private void uploadFile(String filePath) {
          File file = new File(filePath);
          try {
              FileInputStream fileInputStream = new FileInputStream(file);
              URL url = new URL(&quot;http://192.168.1.6/zanborak/sabtenam/upload.php&quot;);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setRequestMethod(&quot;POST&quot;);
              connection.setUseCaches(false);
              connection.setRequestProperty(&quot;Connection&quot;, &quot;Keep-Alive&quot;);
              connection.setRequestProperty(&quot;ENCTYPE&quot;, &quot;multipart/form-data&quot;);
              connection.setRequestProperty(&quot;Content-Type&quot;, &quot;multipart/form-data;boundary=&quot; + boundary);
              connection.setRequestProperty(&quot;uploaded_file&quot;, filePath);
  
              DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
              dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd + &quot;Content-Disposition: form-data;&quot; +
                      &quot; name=\&quot;uploaded_file\&quot;;filename=\&quot;&quot; + filePath + &quot;\&quot;\r\n\r\n&quot;);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              while (bytesRead &gt; 0) {
  
                  dataOutputStream.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  
              }
  
              dataOutputStream.writeBytes(lineEnd);
              dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
          
              if (connection.getResponseCode() == 200) {
                  uploadHandler.post(new Runnable() {
                      @Override
                      public void run() {
                          dialog.dismiss();
                          Toast.makeText(getContext(), &quot;Upload Completed&quot;, Toast.LENGTH_SHORT).show();
                          imageProfile = &quot;&quot;;
                          
                      }
                  });
  
              }
  
              fileInputStream.close();
              dataOutputStream.flush();
              dataOutputStream.close();
  
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>
  <p>and php:</p>
  <p>in this code im trying move the image to &quot;upload&quot; folder...</p>
  <pre><code>    &lt;?php
  include &quot;connect.php&quot;;
  $rand=microtime();
  $path=&quot;upload/&quot;.$rand.($_FILES[&quot;uploaded_file&quot;][&quot;name&quot;]);
  move_uploaded_file($_FILES[&quot;uploaded_file&quot;][&quot;tmp_name&quot;],$path);
  ?&gt;
  </code></pre>