Reputation: 3027
I am trying to write to a text file in Android. My code is as follows (running in a Service):
File log; String state = Environment.getExternalStorageState();
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
log = new File(path, "log.csv");
System.out.println("Opening file");
} else {
System.err.println("Could not write to external storage medium. No log will be created");
return;
}
String line = "stuff";
if (!log.exists()) {
try {
log.createNewFile();
System.out.println("Creating new log file.");
} catch (IOException e) {
System.err.println("Could not create new log file");
e.printStackTrace();
return;
}
}
try {
path.mkdirs();
BufferedWriter bw = new BufferedWriter(new FileWriter(log, true));
bw.append(line);
System.out.println("Appending to log file.");
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
System.err.println("Failed to write to log file");
e.printStackTrace();
return;
}
And I have put the following line in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When I run this, the following is printed in LogCat:
Opening file
Appending to log file.
But I can't find any files with the given name on the phone's memory, despite looking in the desired directory and running a search to see if it's somewhere else instead.
I'm not sure what could be going wrong. Any ideas? Thanks!
Upvotes: 0
Views: 585
Reputation: 6289
i have rooted 4.2.1 GNexus and here's how i find that 'External' directory for 'Movies'...
127|shell@android:/storage # busybox find . -type l // list symlinks in /storage
./sdcard0
./emulated/legacy
shell@android:/storage # ls -l sdcard0 // list the symlink for SDCRD
lrwxrwxrwx root root 2013-02-12 06:57 sdcard0 -> /storage/emulated/legacy
shell@android:/storage # ls -l /storage/emulated/legacy // follow it
lrwxrwxrwx root root 2013-02-12 06:57 legacy -> /mnt/shell/emulated/0
shell@android:/storage # ls -l /mnt/shell/emulated/0
...
drwxrwxr-x root sdcard_rw 2012-11-14 10:56 Alarms
drwxrwxr-x root sdcard_rw 2012-11-14 10:56 Android
drwxrwxr-x root sdcard_rw 2012-12-13 16:58 DCIM
drwxrwxr-x root sdcard_rw 2013-02-05 14:29 Download
drwxrwxr-x root sdcard_rw 2013-01-19 03:30 Evernote
drwxrwxr-x root sdcard_rw 2013-01-10 14:17 LazyList
drwxrwxr-x root sdcard_rw 2013-02-13 08:57 Movies
drwxrwxr-x root sdcard_rw 2013-01-14 09:15 Music
drwxrwxr-x root sdcard_rw 2012-11-14 10:56 Notifications
drwxrwxr-x root sdcard_rw 2013-02-13 08:57 PictureComment
drwxrwxr-x root sdcard_rw 2013-02-13 08:57 Pictures
drwxrwxr-x root sdcard_rw 2012-11-14 10:56 Podcasts
ls -l /mnt/shell/emulated/0/Movies/ <
-rw-rw-r-- root sdcard_rw 517984 2012-12-31 14:53 20121231110404.mp4
-rw-rw-r-- root sdcard_rw 937716 2013-01-02 13:32 20130102133041.mp4
-rw-rw-r-- root sdcard_rw 0 2013-01-06 18:20 20130106181851.mp4
-rw-rw-r-- root sdcard_rw 1015604 2013-01-10 14:17 20130110141521.mp4
-rw-rw-r-- root sdcard_rw 310100 2012-12-13 16:57 me-2139628341.mp4
-rw-rw-r-- root sdcard_rw 212589 2012-12-10 09:40 me-813905104.mp4
-rw-rw-r-- root sdcard_rw 387913 2012-12-10 10:43 me-827018151.mp4
-rw-rw-r-- root sdcard_rw 204403 2012-12-10 10:39 me-902796538.mp4
-rw-rw-r-- root sdcard_rw 796636 2012-12-29 12:25 me-999888777.mp4
-rw-rw-r-- root sdcard_rw 297690 2013-02-13 08:57 me115420782.mp4
Upvotes: 0
Reputation: 3090
If it's a real device (not AVD), then I think it's plugged into your computer via USB. Some settings doesn't allow to browse external storage while it's plugged to a computer. Unplug it, and look for the file.
Upvotes: 1