How to send audio file (mp3 or WAV) from app to server?

I’m trying to send an mp3 file that is a recording that my app does, from the app to a server that I made. Been using volley to send requests that were only with strings, but now I need to send the file.
Been trying to convert the file to byte array and to send it, but it seems that the conversion from the file to byte array is not right.

private byte[] fileToBytes() {
    byte[] bytes = null;
    File audioFile = new File(mPathSave);
    try {

        bytes = Files.readAllBytes(audioFile.toPath());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bytes;
}

private void isCorrectAnswer(MediaRecorder iRecorder, final Button iButton) {
    String url = "myUrl";
    File file = new File(mPathSave);
    try {
        InputStream inFile = new FileInputStream(mPathSave);
        byte[] bytes = fileToBytes();

        JSONObject jsonBody = new JSONObject();
        jsonBody.put("answer", mQuestion.GetmAnswer());
        jsonBody.put("email", "email@email.com");
        jsonBody.put("audio_file", bytes);
        final RequestQueue queue = Volley.newRequestQueue(this);
        RequestFuture<JSONObject> future = RequestFuture.newFuture();
        final JsonObjectRequest jsonRequest =
                new JsonObjectRequest(Request.Method.POST, url, jsonBody,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {

                                iButton.setText(response.toString());
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.print("ERROR!");
                    }
                });
        queue.add(jsonRequest);


    } catch (IOException e) {
        e.printStackTrace();
    }
}

Update:
The file is being converted to this:

[B@5bc86d4

But what i need it to be converted to is something like this:

b’x00x00x00x18ftypmp42x00x00x00x00isommp42x00x00x02xf6moovx00x00x00lmvhdx00x00x00x00xd9Xx87xf9xd9Xx87xf9x00x00x03xe8x00x00nx00x00x01x00x00x01x00x00x00x00x00x00x00x00x00x00x00x00x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x01x00x00x00x00x00x00x00x00x00x00x00x00x00x00@x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x02x00x00x00wmetax00x00x00!hdlrx00x00x00x00x00x00x00x00mdtax00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00+keysx00x00x00x00x00x00x00x01x00x00x00x1bmdtacom.android.versionx00x00x00#ilstx00x00x00x1bx00x00x00x01x00x00x00x13datax00x00x00…

(it’s really long, but you can see the difference…)

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

use multipart request like this private SimpleMultiPartRequest smr; private RequestQueue mRequestQueue; MainActivity: mRequestQueue = Volley.newRequestQueue(this); private void imageUpload(final String imagePath) { smr = new SimpleMultiPartRequest(Request.Method.POST, Globalvariable.BASE_URL + "api/profile/image", new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("Response", response); try { JSONObject jsonObject = new JSONObject(response); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(com.android.volley.error.VolleyError error) { Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); } }); smr.addFile("Image", imagePath); smr.addMultipartParam("Id","multipart/form-data",userid); //MyApplication.getInstance().addToRequestQueue(smr); mRequestQueue.add(smr); }