简体   繁体   English

录制的音频无法保存在/ storage / emulated / 0 / AudioRecorder中(有时会出现保存的文件,有时不会出现)

[英]Recorded Audio Can't be saved in /storage/emulated/0/AudioRecorder (sometimes saved file appear, sometimes not appear)

I want to save my recorded audio at /storage/emulated/0/AudioRecorder, This is my code : 我想将录制的音频保存在/ storage / emulated / 0 / AudioRecorder,这是我的代码:

private String getFilename() {
        File file = new File(Environment.getExternalStorageDirectory(), "/AudioRecorder");
        if (!file.exists()) {
            file.mkdirs();
        }

        createdDate = DateTools.getDate(System.currentTimeMillis(),"dd-MMM-yyyy hh:mm:ss");
        nameFile = createdDate + ".mp4";

        pathFile = (file.getAbsolutePath() + "/" + nameFile);
        return pathFile;
    }

private void startRecording() {
        if(RadioManager.getInstance().isPlaying()){
            RadioManager.getInstance().stopPlayer();
        }
        Toast.makeText(VoiceNoteActivity.this, "Mulai Rekam", Toast.LENGTH_SHORT).show();
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(output_formats);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(getFilename());
        recorder.setOnErrorListener(errorListener);
        recorder.setOnInfoListener(infoListener);
        try {
            recorder.prepare();
            recorder.start();
            isRecord = true;
            countDown.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopRecording() {
        if (null != recorder) {
            countDown.cancel();

            timer = 0;
            seekbar.setProgress(timer);
            try{
                recorder.stop();
                recorder.reset();
                recorder.release();
                recorder = null;
            }catch(RuntimeException stopException){
                //handle cleanup here
                Log.e("Voice Note Activity ","Voice Recording Error : " +stopException.getMessage());
            }

            isRecord = false;
            time.setText("00:00");
            enableButtons(false);
        }
    }

The file sometimes appear in /storage/emulated/0/AudioRecorder, but sometimes not appear (more often to not appear). 该文件有时出现在/ storage / emulated / 0 / AudioRecorder中,但有时不出现(通常不出现)。 Anybody can help ? 有人可以帮忙吗?

Hem.. I got same problem but with picture not voice. 下摆..我遇到了同样的问题,但图片却没有声音。 In my case the picture didn't appear because I haven't throw it to gallery. 就我而言,图片没有出现是因为我没有将其扔到画廊。 Based on the tutorial on android developer website is it the code for throw picture to gallery: 根据android开发人员网站上的教程,将图片丢到图库的代码是:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(fileUri.getPath());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

may be you can use it to voice file. 可能是您可以使用它来语音文件。 but I haven't try to voice file. 但我没有尝试语音文件。

I have solved this by adding this code : 我已经通过添加以下代码解决了这个问题:

File file = new File(getFilename());
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

So, when the file is exist, it doesn't need to create that file again, but when saved file not exist, it needs to create the file. 因此,当文件存在时,不需要再次创建该文件,但是当保存的文件不存在时,则需要创建该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用android AudioRecorder类录制的文件是否保存在某处? - Is the file recorded with android AudioRecorder class saved somewhere? Android - 外部存储中保存的图像不会出现在图库中 - Android - Saved image in external storage doesn't appear in gallery 已保存的视频未出现在图库中 - saved video doesn't appear in gallery 已保存的位图未显示在SD卡上 - Saved Bitmap doesn't appear on SD Card 文件已保存在下载中,但未显示在“下载”应用中 - File saved in downloads but does not appear in Download app 有时在复杂视图中不会出现底部工作表 - Bottom Sheet doesn't appear sometimes in complicated view 将文件保存在/ Download下,其名称为/ storage / emulated / 0 / Download。 有时文件无法访问 - Saving file under /Download gives name /storage/emulated/0/Download. Sometimes the file is not accessable 有时会保存带有字节数组列的解析对象,但字节数组存储不正确 - Sometimes parse object with byte array column is saved but the byte array isn't saved properly 保存到 sdcard 的图像不会出现在 Android 的图库应用中 - Image, saved to sdcard, doesn't appear in Android's Gallery app 有时对于相同的位图,位图另存为黑色图像 - Bitmap saved as black image sometimes for same bitmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM