title: Problems uploading base64 images to server by httpPost
body: <p>I am prototyping an application where I send the whole content of the local SQLite database to a remote MySQL database using JSON and HttpPost.</p>
  
  <p>Everything works fine for text data. 
  Now I added images to the party and I though I could add the images as base64 strings to the JSON I am sending away. My images are 800 x 600 pixels, more or less 500kb in size each. </p>
  
  <p>If I paste the JSON generated by my app manually to a web page, it is fine, I get my images and everything else.</p>
  
  <p>Using the app uploading script, I tried to upload a JSON string with 4 images but the application gets stuck at my progress dialog and Logcat goes on over and over showing this:</p>
  
  <pre><code>11-15 14:32:27.809: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680048-byte allocation
  11-15 14:32:27.840: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20666K/29447K, paused 2ms+3ms
  11-15 14:32:27.879: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 16ms
  11-15 14:32:27.918: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19794K/29447K, paused 22ms
  11-15 14:32:27.918: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680276-byte    allocation
  11-15 14:32:27.958: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20667K/29447K, paused 1ms+4ms
  11-15 14:32:27.997: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 17ms
  11-15 14:32:28.028: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19795K/29447K, paused 17ms
  </code></pre>
  
  <p>and it goes on forever. I bet the images are too big to be sent this way, and/or I am leaking memory somewhere.</p>
  
  <p>What would be a better approach to upload big size images along with JSON data to a server? Otherwise, how can I avoid memory leaks?</p>
  
  <p>The code is pretty standard...</p>
  
  <p>From Cursor to JSON:</p>
  
  <pre><code>private JSONObject get_images_data_JSON(Cursor c) {
  
      JSONObject image_jo = new JSONObject();
  
      //get unit identifier
      long unit_identifier = c.getLong(c.getColumnIndex("_id"));
  
      //set unit details
      try {
  
          image_jo.put("_id", unit_identifier);
          image_jo.put("unit_id", c.getLong(c.getColumnIndex("unit_id")));
  
          //encode blob in Base64 for json parsing
          String encodedImage = Base64.encodeToString(c.getBlob(c.getColumnIndex("image")), Base64.DEFAULT);
  
          image_jo.put("image", encodedImage);
          image_jo.put("caption", c.getString(c.getColumnIndex("caption")));
  
  
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      return image_jo;
  
  }//end get_images_data_JSON
  </code></pre>
  
  <p>and the POST function:</p>
  
  <pre><code>public String postData(JSONArray array) {
  
      String responseMessage = "";
  
      //set connection timeout values
      HttpParams myParams = new BasicHttpParams();
  
      //set timeout in milliseconds until a connection is established
      HttpConnectionParams.setConnectionTimeout(myParams, 10000);
  
      //set timeout for waiting data
      HttpConnectionParams.setSoTimeout(myParams, 10000);
      HttpClient httpclient = new DefaultHttpClient(myParams);
  
      //Get a string out of the JSONArray
      String json = array.toString();
  
      try {
  
          HttpPost httppost = new HttpPost(URL);
          httppost.setHeader("Content-type", "application/json");
  
          StringEntity se = new StringEntity(json);
          se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
          httppost.setEntity(se);
  
          HttpResponse response = httpclient.execute(httppost);
          InputStream inputStream = response.getEntity().getContent();
          responseMessage = inputStreamToString(inputStream);
  
          //log out response from server
          longInfo(responseMessage);
  
      }
      //show error if connection not working
      catch (SocketTimeoutException e) {
          e.printStackTrace();
          responseMessage = "unreachable";
  
      }
  
      catch (ConnectTimeoutException e) {
          e.printStackTrace();
          responseMessage = "unreachable";
  
      }
  
      catch (Exception e) {
          e.printStackTrace();
          responseMessage = "unreachable";
  
      }
  
  
  
      return responseMessage;
  
  }
  </code></pre>
  
  <p>Any help will be greatly appreciated</p>