title: upload image to server in android
body: <p>I want to post an image and text data to server. When I'm sending the data, it only sends the text data, but not the image.</p>
  
  <pre><code>public class MainActivity extends Activity implements
      android.view.View.OnClickListener {
  
  private static final int SELECT_PICTURE = 0;
  Bitmap bm;
  ImageView img;
  EditText edittext;
  Button btn;
  String imgtype, img1;
  JSONObject jobj;
  File fileToUpload = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      if (android.os.Build.VERSION.SDK_INT &gt; 8) {
  
          StrictMode.ThreadPolicy th = new StrictMode.ThreadPolicy.Builder()
                  .permitAll().build();
          StrictMode.setThreadPolicy(th);
  
      }
  
      btn = (Button) findViewById(R.id.button1);
      edittext = (EditText) findViewById(R.id.editText1);
      btn.setOnClickListener(this);
  
      img = (ImageView) findViewById(R.id.imageView1);
      img.setOnClickListener(this);
  }
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
  
      switch (v.getId()) {
      case R.id.imageView1:
          PhotoFromGallery();
          break;
      case R.id.button1:
          // upload();
  
          upload();
  
          break;
      default:
          break;
      }
  
  }
  
  public void PhotoFromGallery() {
      Intent i = new Intent(Intent.ACTION_PICK,
              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  
      startActivityForResult(i, SELECT_PICTURE);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
  
      switch (requestCode) {
      case SELECT_PICTURE:
  
          if (resultCode == Activity.RESULT_OK) {
              Uri selectedImage = data.getData();
              String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
              Cursor cursor = getContentResolver().query(selectedImage,
                      filePathColumn, null, null, null);
              cursor.moveToFirst();
  
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String picturePath = cursor.getString(columnIndex);
              cursor.close();
  
              if (bm != null &amp;&amp; bm.isRecycled()) {
  
                  bm = null;
              }
              bm = BitmapFactory.decodeFile(picturePath);
              img.setImageBitmap(bm);
  
          }
      }
  
  }
  
  private void upload() {
      try {
  
          String a = edittext.getText().toString();
          // img1=img.setImageBitmap(bm);
  
          // URL url = new URL(
          String URL_BASE = "yoururl";
  
          String url1 = String
                  .format(URL_BASE
                          + "description_comment_=%s&amp;member_id_comment_=%s&amp;product_id_comment_=%s&amp;store_id_comment_=%s",
                          a, 1, 191, 55);
  
          URL url = new URL(url1);
  
          HttpURLConnection c = (HttpURLConnection) url.openConnection();
          // this HTTP request will involve input
          c.addRequestProperty("app_key", "rg946A");
          c.addRequestProperty("description", edittext.getText().toString());
          // c.addRequestProperty("categories", SelcectItem);
          c.setDoInput(true);
          // should be PUT or POST to follow convention
          c.setRequestMethod("POST");
          System.out.println(c.getRequestProperties().toString());
          // this HTTP request will involve output
          c.setDoOutput(true);
          // open the HTTP connection
          c.connect();
          OutputStream output = c.getOutputStream();
          // compress and write the image to the output stream
          bm.compress(CompressFormat.JPEG, 100, output);
  
          output.close();
  
          System.out.println(c.getResponseMessage());
          if (c.getResponseMessage().equalsIgnoreCase("OK")) {
              edittext.setText("");
              img.setImageResource(R.drawable.ic_launcher);
  
              Toast.makeText(
                      getApplicationContext(),
                      "Picture Upload Successfully..\n"
                              + "Picture will be available after admin approval",
                      Toast.LENGTH_LONG).show();
          }
  
      } catch (IOException e) {
          // log error
          e.printStackTrace();
          Log.e("ImageUploader", "Error uploading image", e);
      }
      }
  
  }
  </code></pre>