Search code examples
androidaudiodirectorysaving-data

Creating subdirectories within a directory and be able to save data into it


I'm new to android developing, and I'm in need of help. I've got two problems which are:

  1. I don't know how to create two subdirectories inside my main directory. I know how to create subdirectory inside of a directory (File directory = new File(Environment.getExternalStorageDirectory()+"Saling-Wika/Audio&Text Files");), but what I do want is there would be two subdirectories inside of my main subdirectory (The Audio Files & Text Files are different two subdirectories inside the Saling-Wika directory which is the main directory).
  2. I don't know how I'm gonna be able to save my data into the subdirectories I've created. Here is the code of my record module where the audio data is coming from:

    public class RecordModule extends Activity {
    
    Button SpeakBtn, StopBtn;
    private MediaRecorder myAudioRecorder;
    private String outputFile = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recordmodule);
    
        SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
        StopBtn = (Button) findViewById(R.id.StopBtn);
    
        StopBtn.setEnabled(false);
        SpeakBtn.setEnabled(true);
        SimpleDateFormat datetime = new SimpleDateFormat("ddMMyyyyhhmmss");
        String format = datetime.format(new Date());
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + format + ".3gp";
    
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);
    
        SpeakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myAudioRecorder.prepare();
                    myAudioRecorder.start();
                }
    
                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                SpeakBtn.setEnabled(false);
                StopBtn.setEnabled(true);
    
                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_SHORT).show();
            }
        });
    
        StopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAudioRecorder.stop();
                myAudioRecorder.release();
                myAudioRecorder = null;
    
                StopBtn.setEnabled(false);
                SpeakBtn.setEnabled(true);
    
                Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }
    

I want my audio data will be stored into the Audio Files directory, but I don't know how I'm gonna be able to do that. Please help me.


Solution

  • I've finally solved my problems, and here are the solutions I've done:

    String folder_main = "Saling-Wika"
    
    File MainDir = new File (Environment.getExternalStorage();, folder_main);
    if (!MainDir.exists()){
       MainDir.mkdirs();
    }
    
    File f1 = new File (Environment.getExternalStorage() + "/" + folder_main, "Audio Files");
    if (!MainDir.exists()){
       MainDir.mkdirs();
    }
    
    File f2 = new File (Environment.getExternalStorage() + "/" + folder_main, "Text Files");
    if (!MainDir.exists()){
       MainDir.mkdirs();
    }
    }
    

    Here is the code how I've created two subdirectories (Audio & Text Files folders) inside of the main directory (Saling-Wika fodler).

    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Saling-Wika/Audio Files/" + format + ".3gp";
    

    And here is how my audio files are being saved into the Audio Files folder.