androidlistviewsavestoragerecorder

store Audio files in SdCard


Here is my call recording app code store Audio File in internal storage but i Want to Store Audio Files in External Storage. What i will change and files store in Sdcard

I am making recording application that store audio files in storage

 public void recordVoiceCall(String phNumber, String name, String type) {

    File LastingSalesRecordingsDir = new File(Environment.getExternalStorageDirectory(),
            audioFileDirectoryPath);


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

    file_name = "call_" + MyDateTimeStamp.getCurrentDate() + "_" +
            MyDateTimeStamp.getCurrentTimeForFile() + "_" + type + "_" + phNumber + "_";
    try {
        audioFile = File.createTempFile(file_name, ".mp3", LastingSalesRecordingsDir);
    } catch (IOException e) {
        Log.d(MyLogTags.recording, "Recording IOException: " + e.getMessage());
        e.printStackTrace();
    }
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    try {
        file_name = audioFile.getName();

        if (recorder == null) {
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);

            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(audioFile.getAbsolutePath());

            Log.d(MyLogTags.recording, "Recording output formats set.");
        }
    } catch (Exception e) {
        Log.d(MyLogTags.recording, "Recording Exception: " + e);
    }
    try {

        recorder.prepare();
        Log.d(MyLogTags.recording, "Recording Recorder prepared.");
    } catch (IllegalStateException e) {
        deleteVoiceFile(file_name);
        Log.d(MyLogTags.recording, "Recording prepare IllegalStateException: " + e);
    } catch (IOException e) {
        deleteVoiceFile(file_name);
        Log.d(MyLogTags.recording, "Recording prepare IOException: " + e);
    }

    try {
        String state = Environment.getExternalStorageState();
        if (!state.equals(Environment.MEDIA_MOUNTED)) {
            Log.d(MyLogTags.recording, "SD Card is not mounted.  It is " + state + ".");
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        recorder.start();
        Log.d(MyLogTags.recording, "Recording Recorder started for: " + phNumber);
        recordStarted = true;
    } catch (Throwable e) {
        deleteVoiceFile(file_name);
        Log.d(MyLogTags.recording, "Recording start Exception: " + e);
    }
}

Solution

  • You can use Android's default external storage directory to save your audio files.

    File path = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_MUSIC);
    

    You can also use File Class Constructor to create a new folder on SD card.

    File audioFolder = new File(Environment.getExternalStorageDirectory(), 
    "newaudiofolder")
      if (!audioFolder.exists()) {
    boolean success = audioFolder.mkdir()
    if (success) {
        // save the file 
    }
    }
    

    Make sure to add permission in Manifest.xml

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>