title: Android: Uploading Image
body: <p>I am currently trying to upload an image onto the PHP server through Android. Below are the codes:</p>
  
  <p>//segment of codes on Android</p>
  
  <pre><code>                        bm = BitmapFactory.decodeFile(imagePath); //imagePath is the path of the image in my SD card    
                          ByteArrayOutputStream bao = new ByteArrayOutputStream();
                          bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);//compressing image
                          byte[] ba = bao.toByteArray();
                          String ba1 = Base64.encodeBytes(ba);
                          ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
                          nameValuePairs.add(new BasicNameValuePair("image",ba1));
  
                          try{
                              HttpClient client = new DefaultHttpClient();
                              HttpPost post = new HttpPost("http://domain.com/upload_image.php");
                              post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                              HttpResponse res = client.execute(post);
                              HttpEntity entity = res.getEntity();
                              is = entity.getContent();
  
  
                          }catch(Exception e){
                              Log.e("log_tag","Error in http connection "+e.toString());
                          }
  </code></pre>
  
  <p>//segment of codes on PHP server (upload_image.php)</p>
  
  <pre><code>&lt;?php
  $base=$_REQUEST['image'];
  
  // base64 encoded utf-8 string
  $binary=base64_decode($base);
  
  // binary, utf-8 bytes
  header('Content-Type: bitmap; charset=utf-8');
  
  $file = fopen('test.jpg', 'wb');
  
  fwrite($file, $binary);
  
  fclose($file);
  ?&gt;
  </code></pre>
  
  <p>I've failed to upload the image onto the server, in which a test.jpg never shows up on the server. Im running the program from my smartphone, not emulator. </p>