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();
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With