2013-02-13 41 views
0

我正在嘗試寫入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. 

但我不能找到在手機內存中的指定名稱的任何文件,儘管在尋找所需的目錄並運行一個搜索,看看它是否在其他地方代替。

我不確定會出現什麼問題。有任何想法嗎?謝謝!

回答

1

如果它是一個真正的設備(而不是AVD),那麼我認爲它通過USB插入到您的計算機。某些設置在插入計算機時不允許瀏覽外部存儲設備。拔下它,並尋找文件。

+0

感謝。這解決了它。 – Froskoy 2013-02-13 16:45:37

0

我已經根深蒂固4.2.1 GNexus這裏就是我發現,「外部」的「電影」目錄...

127|[email protected]:/storage # busybox find . -type l // list symlinks in /storage       
./sdcard0 
./emulated/legacy 
[email protected]:/storage # ls -l sdcard0  // list the symlink for SDCRD 

lrwxrwxrwx root  root    2013-02-12 06:57 sdcard0 -> /storage/emulated/legacy 

[email protected]:/storage # ls -l /storage/emulated/legacy // follow it       
lrwxrwxrwx root  root    2013-02-12 06:57 legacy -> /mnt/shell/emulated/0 

[email protected]:/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