java.io.FileNotFoundException: 打开失败:EISDIR(是一个目录)

huangapple go评论76阅读模式
英文:

java.io.FileNotFoundException: open failed: EISDIR (Is a directory)

问题

我正在尝试在Android Studio的钢琴应用中使用MediaRecorder录制音频。在寻找关于如何保存文件的答案时,我发现getExternalPublicDirectory已被弃用,所以我使用了context.getExternalFilesDir(null).getAbsolutePath();。现在,当我运行应用并点击录制按钮时,在mediaRecorder.prepare();这一行中出现错误java.io.FileNotFoundException: open failed: EISDIR(是一个目录).. 请帮忙!

private void startRecording() throws IOException {

    try {
        String path = context.getExternalFilesDir(null).getAbsolutePath();
        audioFile = new File(path);
    } catch (Exception e) {
        Log.e("错误标签", "外部存储访问错误" + e.getMessage());
        return;
    }

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setOutputFile(audioFile.getAbsolutePath());

    mediaRecorder.prepare();
    mediaRecorder.start();
}
英文:

I am trying to record audio in my Piano app in android studio using MediaRecorder. After searching for answers on how to save the file as getExternalPublicDirectory is deprecated so i used context.getExternalFilesDir(null).getAbsolutePath(); . Now when i run the app and hit the record button , i get this error java.io.FileNotFoundException: open failed: EISDIR (Is a directory) in the line mediaRecorder.prepare(); .. Please Help!!

private void startRecording() throws IOException {

        try {
            String path = context.getExternalFilesDir(null).getAbsolutePath();
            audioFile = new File(path);
        } catch (Exception e) {
            Log.e("Error Tag", "external storage access error " + e.getMessage());
            return;
        }


        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

         mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         mediaRecorder.setOutputFile(audioFile.getAbsolutePath());

            mediaRecorder.prepare();
            mediaRecorder.start();

答案1

得分: 4

收到预期的java.io.FileNotFoundException: open failed: EISDIR (Is a directory)错误,因为您提供给setOutputFile()的路径指向了一个目录而不是要保存记录数据的文件。

文档中已经很清楚地说明了,

> 设置要生成的输出文件的路径。

请尝试修复您的路径,

String dirPath = context.getExternalFilesDir(null).getAbsolutePath();
String filePath = dirPath + "/recording";          
audioFile = new File(filePath);
英文:

Getting java.io.FileNotFoundException: open failed: EISDIR (Is a directory) is what expected since the path that you're providing to setOutputFile() points to a directory and not a file to save the recorded data.

The documentation is clear,

> Sets the path of the output file to be produced.

Try and fix your path,

String dirPath = context.getExternalFilesDir(null).getAbsolutePath();
String filePath = direPath + "/recording";          
audioFile = new File(filePath);

huangapple
  • 本文由 发表于 2020年5月30日 18:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/62101194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定