致命异常:录音机线程和文件未找到例外

时间:2013-11-27 08:38:53

标签: android file exception audio record

我正在尝试在我的应用程序中实现录音机。我尝试了这段代码,我收到了错误

11-27 08:21:12.394: W/System.err(1476): java.io.FileNotFoundException: /mnt/sdcard/AudioRecorder/1385540472372.mp4: open failed: EACCES (Permission denied)
11-27 08:21:12.414: W/System.err(1476):     at libcore.io.IoBridge.open(IoBridge.java:416)
 11-27 08:21:12.414: W/System.err(1476):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
11-27 08:21:12.454: W/System.err(1476):     at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
11-27 08:21:12.454: W/System.err(1476):     at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
11-27 08:21:12.464: W/System.err(1476):     at com.example.sms.OtherActivity.writeAudioDataToFile(OtherActivity.java:150)
11-27 08:21:12.484: W/System.err(1476):     at com.example.sms.OtherActivity.access$3(OtherActivity.java:141)
11-27 08:21:12.504: W/System.err(1476):     at com.example.sms.OtherActivity$3.run(OtherActivity.java:121)
11-27 08:21:12.524: W/System.err(1476):     at java.lang.Thread.run(Thread.java:856)
11-27 08:21:12.545: W/System.err(1476): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-27 08:21:12.564: W/System.err(1476):     at libcore.io.Posix.open(Native Method)
11-27 08:21:12.564: W/System.err(1476):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-27 08:21:12.614: W/System.err(1476):     at libcore.io.IoBridge.open(IoBridge.java:400)
11-27 08:21:12.614: W/System.err(1476):     ... 7 more
 11-27 08:21:12.614: I/System.out(1476): 
 FATAL EXCEPTION:Audio Recorder Thread

11-27 08:21:12.674: E/AndroidRuntime(1476): java.lang.NullPointerException
11-27 08:21:12.674: E/AndroidRuntime(1476):at com.example.sms.OtherActivity.writeAudioDataToFile(OtherActivity.java:163)
11-27 08:21:12.674: E/AndroidRuntime(1476):     at com.example.sms.OtherActivity.access$3(OtherActivity.java:141)
11-27 08:21:12.674: E/AndroidRuntime(1476):     at com.example.sms.OtherActivity$3.run(OtherActivity.java:121)
11-27 08:21:12.674: E/AndroidRuntime(1476):     at java.lang.Thread.run(Thread.java:856)

我得到文件名为I / System.out(1476):

Filename/mnt/sdcard/AudioRecorder/1385540472372.mp4Start writing to file[S@411b4610

这是我的代码,

 private void startRecording() {

     recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
     RECORDER_SAMPLERATE, RECORDER_CHANNELS,
     RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
     System.out.println("Start recording");
     recorder.startRecording();

     isRecording = true;

     recordingThread = new Thread(new Runnable() {

     public void run() {

     writeAudioDataToFile();
     System.out.println("In run recording");
     }
     }, "AudioRecorder Thread");
     recordingThread.start();
     }

writeAudioDataToFile方法,

 private void writeAudioDataToFile() {
     // Write the output audio in byte

    String filePath=getFilename();
     System.out.print("Filename"+filePath);
     short sData[] = new short[BufferElements2Rec];

     FileOutputStream os = null;
     try {
     os = new FileOutputStream(filePath);
     } catch (FileNotFoundException e) {
     e.printStackTrace();
     }

     while (isRecording) {
     // gets the voice output from microphone to byte format
     recorder.read(sData, 0, BufferElements2Rec);
     System.out.println("Start writing to file" + sData.toString());
     try {
     // writes the data to file from buffer stores the voice buffer
     byte bData[] = short2byte(sData);

     os.write(bData, 0, BufferElements2Rec * BytesPerElement);

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

     try {
     os.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }

getFileName方法,

private String getFilename() {
         String filepath = Environment.getExternalStorageDirectory().getPath();
         File file = new File(filepath, AUDIO_RECORDER_FOLDER);
         if (!file.exists()) {
         file.mkdirs();
         }
         return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
         }

当我点击开始按钮时,会出现此错误,同时启动按钮变为不可见,停止按钮变为可见。任何人都可以告诉我为什么我会收到此错误?我也在清单文件中编写了权限。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

确保您在清单文件中设置了以下权限: android.permission.READ_EXTERNAL_STORAGEandroid.permission.WRITE_EXTERNAL_STORAGE