0

我正在尝试在 Android 中写入文本文件。我的代码如下(在服务中运行):

    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;
    }

我在 AndroidManifest.xml 中添加了以下行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

当我运行它时,LogCat 中会打印以下内容:

Opening file
Appending to log file.

但是我在手机内存中找不到任何具有给定名称的文件,尽管查看了所需的目录并运行搜索以查看它是否在其他地方。

我不确定可能出了什么问题。有任何想法吗?谢谢!

4

2 回答 2

1

如果它是真实设备(不是 AVD),那么我认为它是通过 USB 插入您的计算机的。某些设置不允许在插入计算机时浏览外部存储。拔掉它,然后寻找文件。

于 2013-02-13T16:35:42.520 回答
0

我已经植根 4.2.1 GNexus,这就是我如何找到“电影”的“外部”目录...

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
于 2013-02-13T17:16:13.117 回答