写入不在Android中运行的文件

时间:2013-02-13 16:28:58

标签: android

我正在尝试写入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.

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

我不确定会出现什么问题。有任何想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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