title: Android upload image to server using base64
body: <p>I am uploading image to server using base64 its working fine if image is small but if image is big it gives me memory out exception </p>
  
  <pre><code>try {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
          bitmap.recycle();
          byte[] byteArray = stream.toByteArray();
          stream.close();
          stream = null;
          String ba1 = Base64.encodeToString(byteArray, Base64.DEFAULT);
  
          bitmap = null;
          System.gc(); // immediately free bitmap
  
          byteArray = null;
          System.gc();
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(Data.upload_photo);
  
          JSONObject jsonObj = new JSONObject();
          jsonObj.put("user_id", LoginParser.id);
          int cnt = ba1.length() / 4;
          System.out.println("cnt=&gt;" + cnt);
          jsonObj.put("image", ba1);
  
          jsonObj.put("caption", edt_caption.getText().toString());
          // Create the POST object and add the parameters
  
          /*
           * StringEntity entity = new StringEntity(jsonObj.toString(),
           * HTTP.UTF_8); System.out.println(entity);
           * entity.setContentType("application/json");
           * httppost.setEntity(entity);
           * 
           * HttpResponse response = httpclient.execute(httppost);
           */
  
          StringEntity se = new StringEntity(jsonObj.toString());
          // se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
          // "application/json"));
          se.setContentType("application/json");
          httppost.setEntity(se);
          HttpResponse response = httpclient.execute(httppost);
  
          String result = EntityUtils.toString(response.getEntity());
          System.out.println("=&gt;=&gt;" + result);
          JSONObject jsonObject = new JSONObject(result);
          if (jsonObject.getString("status").equals("success"))
              return "success";
          else
              return jsonObject.getString("message");
      } catch (IOException e) {
          e.printStackTrace();
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>