This may be a simple one for you to crack, but I cant see why this code doesn’t result in creating a new file in my Android external file system? The directory is correctly created but not the file, instead I get an java.io.FileNotFoundException: /mnt/sdcard/Mydir/MyFile, can anyone see what the problem is cause I can’t get my eyes on it?
//Generate a unique filename using date and time
String fileName = "myFile_"+formatter_file_name.format(today)+".txt";
//Get path to root
String root = Environment.getExternalStorageDirectory().toString();
//Create(if not exists) directory in root in which to store the reports
File myDir = new File(root + "/myDir");
myDir.mkdirs();
//Create report file object
File file = new File (myDir, fileName);
//If file already exists delete id(this should not be able to happen)
if (file.exists()) file.delete();
try {
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fout);
// Write the string to the file
osw.write("TEST STRING");
//Ensure that everything has been written to the file and close
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
}
I have the correct permissions in my manifest, and I will insert a check to see if external storage is available for writing but I dont’t think that’s the problem…
Try this