I am trying to write a log file of my app to my external SD card. To figure out how to do this, I have been trying to follow the example HERE.
There is a section about 3/4 of the way down that page that says:
If none of the pre-defined sub-directory names suit your files, you can instead call getExternalFilesDir() and pass null. This returns the root directory for your app's private directory on the external storage.
My device does not have any of those pre-defined directories, and I would rather just as well save my data to the external SD card. Using the following command:
$ ADB shell
shell@android:/ $ cd /mnt
shell@android:/ $ ls -F
d ext_sdcard
You can see my external SD card. So, I created a directory on the external SD card:
shell@android:/ $ cd ext_sdcard
shell@android:/ $ mkdir MyAwesomeApplication
shell@android:/ $ ls -F
d MyAwesomeApplication
Now is when I [sort of] use the code from the Android developers site:
public File getDocumentStorageDir(String filename){
File file = new File("/mnt/ext_sdcard/MyAwesomeApplication", filename);
if(!file.getParentFile().mkdirs()){
Log.i(MainActivity.class.getName(), "Directory Not Created!");
}
return file;
}
When I run my application, it sort of works; I immediately get this error:
E/myPackage.MainActivity: Directory Not Created!
But then it creates the log file anyway, and successfully writes the data to the file.
My issue is that it is always overwriting the log file with new data, it doesn't append, even though I am using:
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
if(isExternalStorageWritable()){
File logfile = getDocumenetStorageDir("mylogfile.log");
PrintWriter logFileWriter = null;
try {
logFileWriter = new PrintWriter(logfile);
logFileWriter.append("stuff ...");
} catch(FileNotFoundException fnfe){
Log.i(MainActivity.class.getName(), fnfe.getMessage());
} finally {
logFileWriter.close();
}
}
Now, when I use this command:
shell@android:/ $ cd MyAwesomeApplication
shell@android:/ $ ls -F
- mylogfile.log
I see my log file where it should be. But when I use this command:
shell@android:/ $ cat mylogfile.log
// only the very latest piece of data is here
What I am I doing incorrectly here?
Thanks