0

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

我的主要斗争是能够将该音频文件保存在内部存储中:我花时间查看了我的 android 编程书籍并在这里阅读了一些问题和答案:如何在内部存储中保存音频文件 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();

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

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

4

1 回答 1

2

我找到了这个问题的答案,我使用了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();

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

于 2018-05-20T12:27:43.180 回答