title: Send image within Json to a webservice in Android
body: <p>I want to send a json object from my android device to the server (in post).</p>
  
  <p>In my json object, I need to add an image in Base64. I cannot use to String to convert my image to Base64 because a String is too short to contain a Base64 encoded file.
  I must use a BytesArray.</p>
  
  <p>How to send something like that to a JSON webservice ?</p>
  
  <pre><code>{
  "emergency":"gsjqsbl",
  "cod_valid":"O",
  "image":{
    "content":"/9j/4AAQSkZJRg ## MY VERY VERY VERY VERY VERY VERY VERY LONG IMAGE IN BASE64 ## BWNn+SV5H8KQADnn9ysrKA1EVX+lNDkSJ8ai8UADCLoAR3TWVlHT95AVvcfwCvD4Hq1joP5NX3Ciat7zyP4VlZW9bnl1sf//Z",
    "content_type":"image/jpg"
  },
  "indoor":"yes",
  "access_conditional":"text",
  "geo_shape":{
    "type":"Point",
    "coordinates":[
       2.0202024,
       45.799005
    ]
  },
  "lastupdate":"",
  "ref_fr_sdis91":"",
  "name":"TEST IN UPPERCASE WITH SPECIAL CHARACTERS ;/*-é@$~æ€",
  "geo_point_2d":"45.799005,2.0202024",
  "source":"soures",
  "objectid":"",
  "aed_location":"Spopopo"
  }
  </code></pre>
  
  <p>I really cannot use String.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT : What i've done yet :</strong></p>
  
  <pre><code> //output stream
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  
    //write text
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
    bufferedWriter.write("json start { blabla: value");
  
    //Write image
    AssetManager assetManager = context.getAssets();
  
    InputStream istr;
    Bitmap bitmap = null;
    try {
      istr = assetManager.open("pictures/defib12.jpg");
      bitmap = BitmapFactory.decodeStream(istr);
    } catch (IOException e) {
      e.printStackTrace();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    outputStream.write(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
  
    //write text
    OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(outputStream);
    bufferedWriter = new BufferedWriter(outputStreamWriter2);
    bufferedWriter.write("json end }");
  
    HttpResponse response = restClient.executePostRequest(pushUrl, outputStream);
  </code></pre>
  
  <p>And : </p>
  
  <pre><code>public HttpResponse executePostRequest(String url, ByteArrayOutputStream outputStream) throws IOException {
  LogWrapper.debug(DmaRestClient.class, "New HttpPost request: " + url);
  
  DefaultHttpClient client = new DefaultHttpClient();
  HttpPost request = new HttpPost(url);
  
  request.setEntity(new ByteArrayEntity(outputStream.toByteArray()));
  request.setHeader("Content-Type", "application/json");
  
  return client.execute(request);
  }
  </code></pre>