1

I'm develop some app with recording voice function and I just want to save it on my external storage, now I know how to save it without a folder but I want that my app creates unique folder for the files I recorded

my code:

public class MainActivity extends Activity {

MediaRecorder recorder = null;
MediaPlayer player = null;
public static String OUTPUT_FILE;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";

}


public void record(View v) {
    try {

        beginRecording();

    } catch (IllegalStateException | IOException e) {
        e.printStackTrace();
    }
}


public void stop(View v) {
        stopRecording();
}


public void play(View v) {
    try {
        beginPlay();
    } catch (IllegalArgumentException | SecurityException
            | IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public void stopPlayback(View v) {
    StopPlay();
}





private void beginRecording() throws IllegalStateException, IOException {
    dictchMediaRecorder();      


    File file = new File(OUTPUT_FILE);


    if(file.exists()){
        file.delete();
    }


    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
    recorder.setAudioEncodingBitRate(9600);
    recorder.setAudioSamplingRate(44100);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.prepare();
    recorder.start();


}



private void beginPlay() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException  {
    ditchMediaPlayer();
    player = new MediaPlayer();
    player.setDataSource(OUTPUT_FILE);
    player.prepare();
    player.start();

}

I'm only added this lines and it's work perfect:

File dir_image;

public static String OUTPUT_FILE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


       dir_image = new  File(Environment.getExternalStorageDirectory()+File.separator+"folderName");
       dir_image.mkdirs();

      OUTPUT_FILE = dir_image.getPath()+"/acapella.wav";
Matan
  • 296
  • 5
  • 24

1 Answers1

0

Add this :

 private File dir_image;

 String myfile="the name of the song.acc";

 dir_image = new  File(Environment.getExternalStorageDirectory()+
                            File.separator+"The name of your folder");
 dir_image.mkdirs();

 File tmpFile = new File(dir_image,myfile);

I'd like to add that you need to add the following to your manifest

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

Or try to add this to your code : instead of this :

OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";

try :

OUTPUT_FILE = Environment.getExternalStorageDirectory()+
                            File.separator+"The name of your folder"+
                            File.separator+"audiorecorder.acc";
mremremre1
  • 1,036
  • 3
  • 16
  • 34
  • Tnx but what about : recorder.setOutputFile(OUTPUT_FILE); and player.setDataSource(OUTPUT_FILE);?? – Matan May 28 '14 at 21:41
  • i think you should try setOutputfile(tmpFile) or (Environment.getExternalStorageDirectory()+ File.separator+"The name of your folder"+ File.separator+"the name of the song.acc"); – mremremre1 May 28 '14 at 21:44
  • i can't beacuse : The method setOutputFile(FileDescriptor) in the type MediaRecorder is not applicable for the arguments (File) – Matan May 28 '14 at 21:49
  • @Matt you can check one of my other answers in which I answer on how to capture and save and image. Look at the code mostly the methods takescreenshot and onpicturetaken and you will see more about files and output streams. Just look for the answer with the most code to find mine. – mremremre1 May 28 '14 at 21:57
  • @Matt http://stackoverflow.com/questions/23921128/take-a-picture-with-camera-and-get-bitmap/23921614#23921614 hope this helps :) – mremremre1 May 28 '14 at 21:57
  • I thank you for that you tried to help me but that not worked for me..:/// sorry – Matan May 28 '14 at 21:59
  • dir_image = new File(Environment.getExternalStorageDirectory()+File.separator+"folderName"); dir_image.mkdirs(); OUTPUT_FILE = dir_image.getPath()+"/audiorecorder.acc"; – Matan May 28 '14 at 22:06
  • I take it back .. I thought a little outside the box and this is what I ended up with your help thanks a lot .. (It's my first time :)) – Matan May 28 '14 at 22:07
  • @Matt nice :) I added some more stuff in my answer that might give you an easier solution without changing much of your code. try it – mremremre1 May 28 '14 at 22:11
  • Cool .. take a look of my solution i'm added it on my question. – Matan May 28 '14 at 22:15
  • A good programmer is measured by the number of letters in its code :) – Matan May 28 '14 at 22:16