英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论