I am trying to create a directory and then write data into a new file on an external SD card. I have previously written files successfully to internal storage, but with this I seem to have 2 problems (well, apart from myself, as this is only my second app!).
Having searched at length on stackoverflow for advice and sample code, I have built the following method:
void filewriter(String fnam,String mdata){
if (useSD) {
//test to see if a directory called AMJ exists on external storage and create one if it does not
try {
File folder = new File(Environment.getExternalStorageDirectory() + "/AMJ");
if (folder.exists() && folder.isDirectory()) {
Toast.makeText(getApplicationContext(), "folder " + folder + " exists", Toast.LENGTH_LONG).show(); //##1
} else {
Toast.makeText(getApplicationContext(), "folder " + folder + " does not exist", Toast.LENGTH_LONG).show(); //##2
folder.mkdirs();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Unable to find or create a directory for route data on SD card", Toast.LENGTH_LONG).show(); //##3
}
//create a file with file name fnam and write String mdata into the AMJ directory
try {
String namFile = Environment.getExternalStorageDirectory() + "/AMJ/" + fnam;
File datfile = new File(namFile);
Toast.makeText(getApplicationContext(), "File is: " + datfile, Toast.LENGTH_LONG).show(); //##4
datfile.createNewFile();
FileOutputStream fOut = new FileOutputStream(datfile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(mdata);
myOutWriter.close();
fOut.close();
Toast.makeText(getApplicationContext(), "Finished writing to SD", Toast.LENGTH_LONG).show(); //##5
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Write failure", Toast.LENGTH_SHORT).show(); //##6
}
}
}
fnam and mdata are simple text such as "myfile" and "This is my test data".
The first problem is that the first time I run this I get the toast message ##2 (folder does not exist) as I expected. However, I expected a directory AMJ to be created so that the next time I run the code I would get message ##1 to say that the directory now exists, but that doesn't happen - it still says that the directory does NOT exist.
The second problem (which may of course just be a consequence of the first) is that the file creation/write does not work and I get the 'catch' message ##6 every time.
My manifest looks like this:
package="com.example.chris.filewritetests">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am running this on an Android Studio emulator.
Can anyone tell me what is wrong here, please?