title: How to send multiple images to server using MultipartEntity from android
body: <p>I am sending Images and Text to a PHP webservice using the following code.</p>
  <pre><code>try {       
      HttpClient httpClient = new DefaultHttpClient();
      HttpContext localContext = new BasicHttpContext();
      HttpPost httpPost = new HttpPost(URL);
  
      MultipartEntity entity = new MultipartEntity(
              HttpMultipartMode.BROWSER_COMPATIBLE);
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      bitmap.compress(CompressFormat.JPEG, 75, bos);
      byte[] data = bos.toByteArray();
  
      entity.addPart(&quot;files[]&quot;,
              new ByteArrayBody(data, &quot;myImage.jpg&quot;));
  
      entity.addPart(&quot;message0&quot;, new StringBody(caption.getText()
              .toString()));
  
      httpPost.setEntity(entity);
      HttpResponse response = httpClient.execute(httpPost,
              localContext);
      BufferedReader reader = new BufferedReader(
              new InputStreamReader(
                      response.getEntity().getContent(), &quot;UTF-8&quot;));
  
      String sResponse = reader.readLine();
      return sResponse;
  } catch (Exception e) {
      if (dialog.isShowing())
          dialog.dismiss();
      Toast.makeText(ImageUpload.this, e.getMessage(),
              Toast.LENGTH_LONG).show();
      Log.e(e.getClass().getName(), e.getMessage(), e);
      return null;
  }
      }
  </code></pre>
  <p>It works perfectly. But this is only for one image. I want to send 5 images.</p>
  <p>Example: <code>Image1 - Text1</code>
  <code>Image2  - Text2</code> etc..</p>
  <p>So I am confused about how to store 5 images one by one and then on button click, send these images and text associated with them to the server.</p>
  <p>I am getting images from the phone's camera.</p>
  <pre><code>Intent intent = new Intent();
              intent.setType(&quot;image/*&quot;);
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(
                      Intent.createChooser(intent, &quot;Select Picture&quot;),
                      PICK_IMAGE);
  
  
  public void onActivityResult_photo(int requestCode, int resultCode,
          Intent data) {
      // TODO Auto-generated method stub
      if (resultCode == RESULT_OK) {
  
          if (data != null) {
              mImageCaptureUri = data.getData();
              display(mImageCaptureUri);
          } else {
              Toast.makeText(CustomTabActivity.mTabHost.getContext(),
                      &quot;No photo selected..&quot;, Toast.LENGTH_SHORT).show();
          }
  
      }
  
  }
  
  
  private String display(Uri mImageCaptureUri2) {
      // TODO Auto-generated method stub
      String base64string = null;
      try {
  
          if (mImageCaptureUri2 != null) {
  
              System.gc();
  
              selectedImagePath = getPath(mImageCaptureUri2);
  
              File filenew = new File(selectedImagePath);
              int file_size = Integer.parseInt(String.valueOf(filenew
                      .length() / 1024));
              if (file_size &lt;= 10000) {
                  PD1 = ProgressDialog.show(
                          CustomTabActivity.mTabHost.getContext(), &quot;&quot;,
                          &quot;Loading...&quot;);
                  Handler refresh = new Handler(Looper.getMainLooper());
  
                  refresh.post(new Runnable() {
                      public void run() {
  
                          PD1.setCancelable(true);
                          Bitmap newbitmap;
                          newbitmap = decodeFile(selectedImagePath);
                          ByteArrayOutputStream bs = new ByteArrayOutputStream();
                          newbitmap.compress(Bitmap.CompressFormat.PNG, 50,
                                  bs);
                          img.setVisibility(View.VISIBLE);
                          img.setImageBitmap(newbitmap);
                          byte[] abc = bitmapToByteArray(newbitmap);
                          if (txt_phototext.getText().toString().equals(&quot;&quot;)) {
                              submit.put(abc, &quot;&quot;);
                          } else {
                              submit.put(abc, txt_phototext.getText()
                                      .toString());
  
                              // executeMultipartPost();
                          }
                          PD1.dismiss();
  
                      }
                  });
  
              } else {
                  AlertDialog.Builder alertbox = new AlertDialog.Builder(
                          CustomTabActivity.mTabHost.getContext());
                  alertbox.setMessage(&quot;Take Image Size Less than 10 MB&quot;);
                  alertbox.setNeutralButton(&quot;Ok&quot;,
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface arg0,
                                      int arg1) {
                                  finish();
                              }
                          });
                  alertbox.show();
              }
  
          } else {
              System.out.println(&quot;===============NULL========&quot;);
          }
  
      } catch (Exception e) {
          // // TODO Auto-generated catch block
          // e.printStackTrace();
      }
      return base64string;
  }
  
  
      static Bitmap decodeFile(String str) {
      try {
          // decode image size
          BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(new FileInputStream(str), null, o);
  
          // Find the correct scale value. It should be the power of 2.
          final int REQUIRED_SIZE = 70;
          int width_tmp = o.outWidth, height_tmp = o.outHeight;
          int scale = 1;
          while (true) {
              if (width_tmp / 2 &lt; REQUIRED_SIZE
                      || height_tmp / 2 &lt; REQUIRED_SIZE)
                  break;
              width_tmp /= 2;
              height_tmp /= 2;
              scale++;
          }
  
          // decode with inSampleSize
          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize = scale;
          return BitmapFactory.decodeStream(new FileInputStream(str), null,
                  o2);
      } catch (FileNotFoundException e) {
      }
      return null;
  }
  
  public static byte[] bitmapToByteArray(Bitmap bitmap) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
      byte[] bitmapdata = bos.toByteArray();
      return bitmapdata;
  }
  </code></pre>