标题翻译
How to save an audio file in storage in Android Studio?
问题
我正在开发一个安卓钢琴应用,其中包含录音功能。它使用设备麦克风进行录音,然后将录制的文件保存到设备存储中。
我应该如何在安卓应用中正确保存音频文件?我无法将文件保存到设备内部。目前在我停止录音后,我收到一个Toast消息,内容为“Added file null”。在Toast消息中,uri对象给我一个空值。我做错了什么?
private void startRecording() throws IOException {
try {
String direPath = context.getExternalFilesDir(null).getAbsolutePath();
String filePath = direPath + "/recording";
audioFile = new File(filePath);
} 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();
}
private void stopRecording(){
if(mediaRecorder != null)
mediaRecorder.stop();
saveToDisk();
}
public void saveToDisk() {
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
{
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audioFile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
}
Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
Toast.makeText(this, "Added File " + uri, Toast.LENGTH_LONG).show();
}
英文翻译
I am working on a Piano App in android which has a record functionality. It uses the device mic to record and then saves the recorded file to the device memory.
How do I correctly save a audio file in storage in an android App? I am unable to save the file inside the device. Currently I am getting a Toast message "Added file null" after I stop the recording. The uri object is giving me a null value in the Toast message. What am I doing wrong?
private void startRecording() throws IOException {
try {
String direPath = context.getExternalFilesDir(null).getAbsolutePath();
String filePath = direPath + "/recording";
audioFile = new File(filePath);
} 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();
}
private void stopRecording(){
if(mediaRecorder != null)
mediaRecorder.stop();
saveToDisk();
}
public void saveToDisk() {
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
{
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audioFile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
}
Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
Toast.makeText(this, "Added File " + uri, Toast.LENGTH_LONG).show();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论