2017-06-17 25 views
0

私人目錄內的一個子目錄我已經創建了一個目錄是這樣的:如何創建Android的

File mydir = context.getDir("my_private_dir", Context.MODE_PRIVATE); 

現在我想創建另一個目錄中my_private_dir爲此,我試着做這樣說:

File file = new File(mydir.getAbsolutePath()+ File.separator + date); 
      if (file.mkdir()) 
       Log.d(context.getClass().getSimpleName()," date dir created"); 

我不知道爲什麼這不起作用。該子目錄根本沒有被創建。

我希望所有的目錄都是私人的,只有我的應用程序應該能夠訪問它。所以我想讓父目錄保密,並在其中創建子目錄通常就足夠了。但我無法理解爲什麼子目錄沒有被創建。

任何人都可以請幫我解決這個問題嗎?

感謝提前:)

問候

+0

「我試着做這種方式」 - 使用'新文件(mydir.getAbsolutePath(),日期)'。 「子目錄根本沒有被創建」 - 你究竟是如何確定**的? 「我希望所有的目錄都是私人的,只有我的應用程序能夠訪問它 - 」[內部存儲]上的所有內容(https://commonsware.com/blog/2014/04/07/storage-situation-internal -storage.html)是私有的,它包含'getDir()'。 – CommonsWare

回答

0

嘗試使用mkdirs()代替的mkdir()

File file = new File(mydir.getAbsolutePath()+ File.separator + date); 
     if (file.mkdirs()) 
      Log.d(context.getClass().getSimpleName()," date dir created"); 
+0

非常感謝,幫助我創建子目錄的人。但你能幫我理解爲什麼mkdirs()和爲什麼不mkdir()? –