尝试将音频文件上传到服务器时出现Android问题

Posted

技术标签:

【中文标题】尝试将音频文件上传到服务器时出现Android问题【英文标题】:Android issue while trying to Upload audio file to server 【发布时间】:2014-01-23 08:42:37 【问题描述】:

我正在尝试在录制完成后将音频文件上传到服务器。我已将 CurrentDate 和 Time 设置为音频文件名,如下面的代码所示。

 private String getFilename() 
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath, AUDIO_RECORDER_FOLDER);

        SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MMM-yyyy hh-mm-ss");
        String currentDateandTime = sdfDate.format(new Date());

        if (!file.exists()) 
            file.mkdirs();
        
        return (file.getAbsolutePath() + "/" + currentDateandTime + file_exts[currentFormat]);

所以现在文件将作为 23-Jan-2014 12-34-55.mp4 存储在 sdcard 中。

private void startRecording() 
        recorder = new MediaRecorder();
        recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(output_formats[currentFormat]);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(getFilename());
        recorder.setOnErrorListener(errorListener);
        recorder.setOnInfoListener(infoListener);
        try 
            recorder.prepare();
            recorder.start();
         catch (IllegalStateException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
    

    private void stopRecording() 
        if (null != recorder) 
            recorder.stop();
            recorder.reset();
            recorder.release();
            recorder = null;
            doAudioFileUpload(getFilename());
        
    

    private void doAudioFileUpload(String path) 
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = path;

        System.out.println("Inside of doupload nd path is === " + path);

        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024 * 1024;
        String urlString = "http://server-link/folder-name/upload_audio.php";
        try 
            // ------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) 
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.e("Debug", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
         catch (MalformedURLException ex) 
            Log.e("Debug", "error: " + ex.getMessage(), ex);
         catch (IOException ioe) 
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        
        // ------------------ read the SERVER RESPONSE
        try 
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) 
                Log.e("Debug", "Server Response " + str);
            
            inStream.close();

         catch (IOException ioex) 
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        
     

现在我已经使用下面的行将我的录音存储在 SD 卡中

recorder.setOutputFile(getFilename());

及以下用于将音频文件上传到服务器的行。

doAudioFileUpload(getFilename());

所以它使用相同的 getFilename() 来实现这两个目的。现在问题出现在这里,当第一次recorder.setOutputFile(getFilename()); 调用它时,它将文件名存储在 sd 卡中为23-Jan-2014 12-34-55.mp4,并且在录制 20 秒后,当这条线调用将音频上传到服务器时,它应该上传 23-Jan-2014 12-34-15.mp4 这个文件,但是其中它正在搜索23-Jan-2014 12-34-55.mp4 文件并给我找不到文件的错误。

所以基本上在录制开始后,当我调用 doAudioFileUpload(getFilename()); 时,它开始搜索 currentDateandTime 文件而不是我录制的文件。

我尝试使用一些动态名称上传文件,它工作正常,但是当我尝试对这个日期和时间做同样的事情时,它给出了错误。

请帮我解决这个问题。任何帮助,将不胜感激。提前谢谢...

编辑我的Logcat在下面

01-23 14:12:54.716: I/System.out(9279): Inside of doupload nd path is === /mnt/sdcard/AudioRecorder/23-Jan-2014 02-12-54.mp4
01-23 14:12:54.765: E/Debug(9279): error: /mnt/sdcard/AudioRecorder/23-Jan-2014 02-12-54.mp4: open failed: ENOENT (No such file or directory)
01-23 14:12:54.765: E/Debug(9279): java.io.FileNotFoundException: /mnt/sdcard/AudioRecorder/23-Jan-2014 02-12-54.mp4: open failed: ENOENT (No such file or directory)
01-23 14:12:54.765: E/Debug(9279):  at libcore.io.IoBridge.open(IoBridge.java:448)
01-23 14:12:54.765: E/Debug(9279):  at java.io.FileInputStream.<init>(FileInputStream.java:78)
01-23 14:12:54.765: E/Debug(9279):  at iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:293)
01-23 14:12:54.765: E/Debug(9279):  at iqualtech.skirr.Record_AudioPG.stopRecording(Record_AudioPG.java:271)
01-23 14:12:54.765: E/Debug(9279):  at iqualtech.skirr.Record_AudioPG.access$0(Record_AudioPG.java:265)
01-23 14:12:54.765: E/Debug(9279):  at iqualtech.skirr.Record_AudioPG$4.onClick(Record_AudioPG.java:124)
01-23 14:12:54.765: E/Debug(9279):  at android.view.View.performClick(View.java:3517)
01-23 14:12:54.765: E/Debug(9279):  at android.view.View$PerformClick.run(View.java:14155)
01-23 14:12:54.765: E/Debug(9279):  at android.os.Handler.handleCallback(Handler.java:605)
01-23 14:12:54.765: E/Debug(9279):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 14:12:54.765: E/Debug(9279):  at android.os.Looper.loop(Looper.java:154)
01-23 14:12:54.765: E/Debug(9279):  at android.app.ActivityThread.main(ActivityThread.java:4624)
01-23 14:12:54.765: E/Debug(9279):  at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:12:54.765: E/Debug(9279):  at java.lang.reflect.Method.invoke(Method.java:511)
01-23 14:12:54.765: E/Debug(9279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-23 14:12:54.765: E/Debug(9279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-23 14:12:54.765: E/Debug(9279):  at dalvik.system.NativeStart.main(Native Method)
01-23 14:12:54.765: E/Debug(9279): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-23 14:12:54.765: E/Debug(9279):  at libcore.io.Posix.open(Native Method)
01-23 14:12:54.765: E/Debug(9279):  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-23 14:12:54.765: E/Debug(9279):  at libcore.io.IoBridge.open(IoBridge.java:432)
01-23 14:12:54.765: E/Debug(9279):  ... 16 more
01-23 14:12:54.766: D/AndroidRuntime(9279): Shutting down VM
01-23 14:12:54.766: W/dalvikvm(9279): threadid=1: thread exiting with uncaught exception (group=0x40e14258)
01-23 14:12:54.771: E/AndroidRuntime(9279): FATAL EXCEPTION: main
01-23 14:12:54.771: E/AndroidRuntime(9279): java.lang.NullPointerException
01-23 14:12:54.771: E/AndroidRuntime(9279):     at iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:342)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at iqualtech.skirr.Record_AudioPG.stopRecording(Record_AudioPG.java:271)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at iqualtech.skirr.Record_AudioPG.access$0(Record_AudioPG.java:265)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at iqualtech.skirr.Record_AudioPG$4.onClick(Record_AudioPG.java:124)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.view.View.performClick(View.java:3517)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.view.View$PerformClick.run(View.java:14155)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.os.Handler.handleCallback(Handler.java:605)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.os.Looper.loop(Looper.java:154)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at android.app.ActivityThread.main(ActivityThread.java:4624)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-23 14:12:54.771: E/AndroidRuntime(9279):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

这是你需要做的,就像你调用getFileName()的方法一样,它实际上通过添加当前时间和日期来实现文件名,所以第一次调用时,它会给你说01-01-2014 12-12-20.mp420 秒录制后,当您尝试通过 doUploadFile(getFileName()) 上传文件时,它会再次调用该方法并返回一个 renewed 文件名,该文件名将在 20 秒后为 01-01-2014 12-12-40.mp4 。所以这是你调用相同方法两次的问题,只需使用一次,这是你的最终代码。

String myFileName = "";

@Override onCreate()
    //Your recording process
    //Your uploading process

private void startRecording() 
    myFileName = getFilename();
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(myFileName);
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);
    try 
        recorder.prepare();
        recorder.start();
     catch (IllegalStateException e) 
        e.printStackTrace();
     catch (IOException e) 
        e.printStackTrace();
    


private void stopRecording() 
    if (null != recorder) 
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
        doAudioFileUpload(myFileName);
    

【讨论】:

这段代码工作正常,但它正在覆盖文件意味着它一次又一次地只创建一个文件。 对于每条记录,您需要一个简单的新名称,请检查编辑以获取答案。您只需在录制开始时调用 getFileName() 而不是 onCreate() :) 很高兴我能帮上忙 :) 如果可以的话,我会给你 100 个。说真的,你已经给了很多时间来回答这个问题。真的很感谢你。再次感谢。 这就是我们在这里的原因【参考方案2】:

我认为您正在尝试记录,保存到 sccard 然后上传到服务器,对吧?

那么,为什么不尝试改用FileObserver 呢?使用FileObserver,您可以监听目录中的任何文件操作,获取最后添加的文件(及其元数据),然后使用服务上传。

 // set up a file observer to watch this directory on sd card 
 observer = new FileObserver(pathToWatch) 

     @Override
     public void onEvent(int event, String file) 
         if(event == FileObserver.CREATE && !file.endWith(".mp4")) 
             Log.d(TAG, "File created [" + pathToWatch + file + "]");       
             Toast.makeText(getBaseContext(), file + " was saved!",Toast.LENGTH_LONG).show();                 
             // upLoadFileToServer();
         
     
 ;

 observer.startWatching(); //START OBSERVING 

【讨论】:

以上是关于尝试将音频文件上传到服务器时出现Android问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试将文件(图像)上传到服务器时出现错误 403

尝试将生成的 pdf 文件上传到 Azure Blob 存储时出现 404 错误

使用 XAMPP 将图像上传到服务器时出现问题

第一次尝试将应用上传到 Google Play 时出现错误“您的 Android App Bundle 使用错误的密钥签名。”

将 Laravel 项目上传到服务器时出现错误 500

使用预签名 URL 将文件上传到 Amazon S3 时出现 CORS 错误