【问题标题】:How to save an audio recorded in internal storage如何保存在内部存储中录制的音频
【发布时间】:2018-10-28 16:12:17
【问题描述】:

我正在开发的安卓应用程序;它有一个录音选项;我希望它将新的音频录制文件保存在设备的内部存储中,这样用户和其他应用程序都无法访问这些录制的音频文件,除非他们打开我的应用程序。

我的主要斗争是能够将该音频文件保存在内部存储中:我花时间查看我的 android 编程书籍并在这里阅读了一些问题和答案:How to save an audio file in internal storage androidhttps://developer.android.com/guide/topics/data/data-storage#filesInternalhttps://www.tutorialspoint.com/android/android_internal_storage.htm 但是不幸的是,我从那里得到的东西根本无法解决我的问题。

我用来记录的代码如下:

        private MediaRecorder rec;
        private String file_folder="/scholar/";


          String 
          file_path=Environment.getExternalStorageDirectory().getPath();

            File file= new File(file_path,file_folder);

            Long date=new Date().getTime();
            Date current_time = new Date(Long.valueOf(date));

            rec=new MediaRecorder();

            rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            rec.setAudioChannels(1);
            rec.setAudioSamplingRate(8000);
            rec.setAudioEncodingBitRate(44100);
            rec.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
            rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            if (!file.exists()){
                file.mkdirs();
            }
            /*the file here this one:File file= new File(file_path,file_folder);
            it means the value of the outputfile is the one which will be provided by 
            rec.setOutputFile() method, so it will be saved as this name: 

            file.getAbsolutePath()+"/"+"_"+current_time+".amr"

            */
            rec.setOutputFile(file.getAbsolutePath()+"/"+"_"+current_time+".amr");

            try {
                rec.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(Recording_Service.this,"Sorry! file creation failed!",Toast.LENGTH_SHORT).show();
                return;
            }
            rec.start();

               rec.stop();
               rec.reset();

不幸的是,我在那里所做的是将我的文件保存在外部存储上,这意味着在录制后,该文件在设备文件资源管理器中对每个人都可见,他们甚至可以删除该文件。

所以!拜托,我需要你们的帮助,如果有任何帮助,我将不胜感激。谢谢!

【问题讨论】:

  • getFilesDir() 有什么问题? (您链接到的第一个答案中的解决方案)
  • +"/"+"_"+current_time+".amr" 相当粗略的代码!请告诉它评估哪个文件名。请值。
  • if (!file.exists()){ file.mkdirs(); }。检查 mkdirs() 的返回值!改为if (!file.exists()) if (!file.mkdirs()){ Toast(...sorry could not create directory ...); return;}
  • } catch (IOException e) { e.printStackTrace(); } 如果你捕捉到异常然后向用户显示一个 Toast() 。并返回;你现在是在盲目地调用 .start()。
  • @Okas 每当我尝试使用 getFilesDir() 时,我的应用程序都会崩溃: File file= new File(getFilesDir().getAbsolutePath()+"/.scholar"); if (!file.exists()){ file.mkdirs(); } rec.setOutputFile(new File(file+"/"+"ab"+sdf+"_o.amr"));或者可能是我没有正确使用它。

标签: android android-mediarecorder android-internal-storage


【解决方案1】:

我已经找到了这个问题的答案,我用getFilesDir();

       private MediaRecorder rec;
       String file_path=getApplicationContext().getFilesDir().getPath();

            File file= new File(file_path);

            Long date=new Date().getTime();
            Date current_time = new Date(Long.valueOf(date));

            rec=new MediaRecorder();

            rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            rec.setAudioChannels(1);
            rec.setAudioSamplingRate(8000);
            rec.setAudioEncodingBitRate(44100);
            rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            if (!file.exists()){
                file.mkdirs();
            }

            String file_name=file+"/"+current_time+".3gp";
            rec.setOutputFile(file_name);

         try {
                    rec.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(Recording_Service.this,"Sorry! file creation failed!"+e.getMessage(),Toast.LENGTH_SHORT).show();
                    return;
                }
                rec.start();

           rec.stop();
           rec.reset();

感谢所有试图帮助我解答的人。让我们继续享受编码的乐趣,让我们的世界变得更加甜蜜愉快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多