how to save the recorded audio files in another folder programmatically?

17.3k views Asked by At

i'm trying to save the recorded audio files in a folder that i wanted it to be rather then the default folder. but somehow i failed to do so.

my code:

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/sound_"+ String.valueOf(System.currentTimeMillis()) + ".amr"));
recordIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(recordIntent, RESULT_OK);

it did calls the voice recorder app. and also when i press the stop button, it return to my app and have a toast appeared saying its saved. but, rather then saving in my Record folder, it save in the default folder.

i realized that there is error msg in the logcat :

01-29 01:34:23.900: E/ActivityThread(10824): Activity com.sec.android.app.voicerecorder.VoiceRecorderMainActivity has leaked ServiceConnection com.sec.android.app.voicerecorder.util.VRUtil$ServiceBinder@405ce7c8 that was originally bound here

i'm not sure what went wrong as the code works when i call the camera app.

3

There are 3 answers

0
starvi On BEST ANSWER

i've found a way to solve this problem, even though it takes a round to do it rather then getting to the point straight, but its the best that i've got and it also work.

instead of calling the voice recorder app with extra included, i just call it without any input :

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(recordIntent, 1111);

then, add an onActivityResult, with the request code == 1111 (depends on what you put) and retrieve the last modified file that consist of the extension "3ga" from the default folder of recorder "Sounds"

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1111)
    {
         File folder = new File(Environment.getExternalStorageDirectory(), "/Sounds");
         long folderModi = folder.lastModified();

    FilenameFilter filter = new FilenameFilter() 
    {
        public boolean accept(File dir, String name) 
        {
            return (name.endsWith(3ga));
        }
    };

    File[] folderList = folder.listFiles(filter);

    String recentName = "";

    for(int i=0; i<folderList.length;i++)
    {
        long fileModi = folderList[i].lastModified();

        if(folderModi == fileModi)
        {
            recentName = folderList[i].getName();
        }
    }
}

this way, i can get the name of the file and also do the modification (e.g renaming) with it.

hope this helps other people. =)

3
Abhi On

Do in this way, Record with MediaRecorder:

To start Recording:

public  void startRecording()
        {


                MediaRecorder recorder = new MediaRecorder();

                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setOutputFile(getFilename());

                recorder.setOnErrorListener(errorListener);
                recorder.setOnInfoListener(infoListener);

                try 
                {
                        recorder.prepare();
                        recorder.start();
                } 
                catch (IllegalStateException e) 
                {
                        e.printStackTrace();
                } catch (IOException e) 
                {
                        e.printStackTrace();
                }
        }

To Stop:

 private void stopRecording()
    {


            if(null != recorder)
            {     
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                   recorder = null;
            }

For Selected Folder:

 private String getFilename()
        {
                String filepath = Environment.getExternalStorageDirectory().getPath();
                File file = new File(filepath,AUDIO_RECORDER_FOLDER);

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

                return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
        }
0
Sara Zakizadeh On

I used this way before and it is Ok for me!

private MediaRecorder mRecorder = null;
    public void startRecording() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(getFilename());
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mRecorder.start();  
        }
    }

to Stop recording:

        public void stopRecording() {

        if (mRecorder != null) {
            mRecorder.stop();
            timer.cancel();
            mRecorder.release();
            mRecorder = null;
    }
    }

To save file:

        @SuppressLint("SdCardPath")
    private String getFilename() {
         file = new File("/sdcard", "MyFile");

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

        return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
    }

if you want to delete folder after recording use this in function of stopping:

    boolean deleted = file.delete();

I hope it can be helpful.