Android image upload AWS Server

Viewed 610

Using following code to upload image on server -

final InputStream fileInputStream = MyApplication.getInstance().getContentResolver().openInputStream(imageFile);
  bitmap = BitmapFactory.decodeStream(fileInputStream);
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
  final byte[] bitmapData = byteArrayOutputStream.toByteArray();

  MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

  // Add binary body
  if (bitmap != null) {
    ContentType contentType = ContentType.create("image/png");
    builder.addBinaryBody("", bitmapData, contentType, "");

    final HttpEntity httpEntity = builder.build();
    StringRequest request =
      new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
      }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
      }) {
        @Override
        public byte[] getBody() throws AuthFailureError {
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          try {
            httpEntity.writeTo(bos);
          } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
          }
          return bos.toByteArray();
        }
      };

Which is uploading image fine but adding body header in the file

--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U
Content-Disposition: form-data; name=""; filename=""
Content-Type: image/png
âPNG
....
--0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U--

If I remove that particular content from the file, the image can be easily used as PNG. Is there a way to only upload PNG file part to the server?

1 Answers
Related