2

I'm working on an android app and part of it records audio. My code works fine and it saves the recorded audio in the SD card using the following code:

String mediaFile = null;
mediaFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myRecordings.mp3";

But I want to create a folder with the app name and be able to save recorded audio to that folder. Can someone tell me how to do that? I succeeded in creating the folder but I don't know how to move the mediaFile in there:

String newFolder = "/FolderName";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + newFolder);
myNewFolder.mkdir();
mediaFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myRecordings.mp3"

Appreciate any help. Thanks

Amanda Slayer
  • 25
  • 2
  • 8

1 Answers1

3

Just add that folder to the path. Like this:

String sep = File.separator; // Use this instead of hardcoding the "/"
String newFolder = "FolderName";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + sep + newFolder);
myNewFolder.mkdir();
mediaFile = Environment.getExternalStorageDirectory().toString() 
  + sep + newFolder + sep + "myRecordings.mp3";
Christoffer
  • 7,470
  • 9
  • 39
  • 55
  • Great @AmandaSlayer! You should mark the answer as accepted, to signal that the question has been solved. Good luck with your app! – Christoffer Oct 26 '14 at 00:48