Sterling Diaz
Sterling Diaz

Reputation: 3875

How can I send an audio file to be store in a server

I want to know how can I convert and audio file store in the SD Card of an android device, to send the audio to the server via internet. I have look a lot for it, but everything is different.

I have and .3gp audio format, and it does not matther the format, I just want to get the file in the server directory I want it to be. Any guide, tutorial, comments, please. I would greatly appreciate it.

Upvotes: 0

Views: 4201

Answers (2)

Raffaele
Raffaele

Reputation: 20875

You can use an HttpClient to POST your music file to some server application.

For example, in PHP, you will be able to read the file by $_FILE['key']. The actual storing and processing depends on your specific requirements. Also, you don't specify if you intend the server to output the transcoded file back.

For transcoding the audio data you may use FFmpeg for instance.

Upvotes: 0

Sayyam
Sayyam

Reputation: 959

I wrote this piece of code to send a file to server:

path = the absolute path to file you want to send. remaining params seems self explanatory.

    public static String sendFileToServer(String path, String urlString,
        String mimeType, String id) throws Exception {


    try {

        String URL = "", file_name = "";
        if (!path.equals("")) {
            file_name = path.substring(path.lastIndexOf("/") + 1);
        }

        File directory = new File(path);

        byte[] data = new byte[(int) directory.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(directory);
            fileInputStream.read(data);

        } catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
            e.printStackTrace();
        } catch (IOException e1) {
            System.out.println("Error Reading The File.");
            e1.printStackTrace();
        }

        URL = Constants.WEB_ROOT_URL + urlString;
        Log.v("AppEngine Manager", "Image Upload URL: " + URL);

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URL);
        ByteArrayBody bab = new ByteArrayBody(data, file_name);
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        reqEntity.addPart("media_type", new StringBody(mimeType));
        reqEntity.addPart("file_name", new StringBody(file_name));
        reqEntity.addPart("file_id", new StringBody(file_name));
        reqEntity.addPart("file", bab);

        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }


        return s + "";

    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
        return "exception";
    }
}

Upvotes: 2

Related Questions