Search code examples
androidandroid-mediarecorderandroid-internal-storage

How to save an audio recorded in internal storage


The android application I am working on; it has an option of audio-recording; I want it to save a new audio recorded file in internal storage of a device so that no neither user nor other applications will be able to access to those recorded audio files, unless they open my application.

My main struggle is to be able to save that audio file in internal storage: I took my time to review in my android programming books and read some questions and answers here : How to save an audio file in internal storage android and https://developer.android.com/guide/topics/data/data-storage#filesInternal and https://www.tutorialspoint.com/android/android_internal_storage.htm but unfortnately things I am getting from there are not settling my problem at all.

The codes that I am using to record are here below :

        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();

Unfortunately what I am doing there, is saving my file on external storage, it means after recording, the file is visible to everyone in a device file explorer, they can even delete the file.

So! Please, I need your help guys, if any help, I will appreciate it. Thanks!


Solution

  • I had found the answer of this question, I used 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();
    

    Thanks for everyone who tried to help me for the answer. Let's continue to enjoy coding to make our world more sweet pleasant and enjoyable.