0

我想知道如何在 Android 设备的 SD 卡中转换和存储音频文件,以通过互联网将音频发送到服务器。我找了很多,但一切都不一样。

我有 .3gp 音频格式,它与格式无关,我只想在我想要的服务器目录中获取文件。任何指南,教程,评论,请。我将不胜感激。

4

2 回答 2

2

我编写了这段代码来将文件发送到服务器:

path = 要发送的文件的绝对路径。剩余的参数似乎不言自明。

    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";
    }
}
于 2012-09-02T20:02:38.653 回答
0

您可以HttpClientPOST音乐文件用于某些服务器应用程序。

例如,在 PHP 中,您将能够通过$_FILE['key']. 实际存储和处理取决于您的具体要求。此外,您没有指定是否希望服务器将转码后的文件输出回来。

例如,为了对音频数据进行转码,您可以使用FFmpeg

于 2012-09-02T20:03:48.510 回答