这是我的通话记录应用程序代码存储音频文件在内部存储,但我想在外部存储中存储音频文件。我将更改和文件存储在Sdcard中
我正在制作将音频文件存储在存储
中的录制应用程序 public void recordVoiceCall(String phNumber, String name, String type) {
File LastingSalesRecordingsDir = new File(Environment.getExternalStorageDirectory(),
audioFileDirectoryPath);
if (!LastingSalesRecordingsDir.exists()) {
LastingSalesRecordingsDir.mkdirs();
}
file_name = "call_" + MyDateTimeStamp.getCurrentDate() + "_" +
MyDateTimeStamp.getCurrentTimeForFile() + "_" + type + "_" + phNumber + "_";
try {
audioFile = File.createTempFile(file_name, ".mp3", LastingSalesRecordingsDir);
} catch (IOException e) {
Log.d(MyLogTags.recording, "Recording IOException: " + e.getMessage());
e.printStackTrace();
}
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
try {
file_name = audioFile.getName();
if (recorder == null) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audioFile.getAbsolutePath());
Log.d(MyLogTags.recording, "Recording output formats set.");
}
} catch (Exception e) {
Log.d(MyLogTags.recording, "Recording Exception: " + e);
}
try {
recorder.prepare();
Log.d(MyLogTags.recording, "Recording Recorder prepared.");
} catch (IllegalStateException e) {
deleteVoiceFile(file_name);
Log.d(MyLogTags.recording, "Recording prepare IllegalStateException: " + e);
} catch (IOException e) {
deleteVoiceFile(file_name);
Log.d(MyLogTags.recording, "Recording prepare IOException: " + e);
}
try {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Log.d(MyLogTags.recording, "SD Card is not mounted. It is " + state + ".");
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
recorder.start();
Log.d(MyLogTags.recording, "Recording Recorder started for: " + phNumber);
recordStarted = true;
} catch (Throwable e) {
deleteVoiceFile(file_name);
Log.d(MyLogTags.recording, "Recording start Exception: " + e);
}
}
答案 0 :(得分:1)
您可以使用Android的默认外部存储目录来保存音频文件。
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
您还可以使用File Class Constructor在SD卡上创建新文件夹。
File audioFolder = new File(Environment.getExternalStorageDirectory(),
"newaudiofolder")
if (!audioFolder.exists()) {
boolean success = audioFolder.mkdir()
if (success) {
// save the file
}
}
确保在Manifest.xml中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>