2015-12-29 75 views
0

我正在製作一個應用程序,我必須在內部保存視頻文件(在應用程序內存中,當卸載應用程序時,所有的文件也應該被卸載)。爲此,我閱讀了許多文章並搜索了很多,發現了不同的解決方案,之後我做了一個方法,還有我寫這裏的代碼是代碼爲什麼ContextWrapper.getDir()返回的文件總是存在?

public static boolean checkIfAlreadyDownloaded(Context context, String rhymeName) { 
    ContextWrapper cw = new ContextWrapper(context); 
    File rhymeDirectory = cw.getDir(Constants.INTERNAL_DIRECTORY_NAME, Context.MODE_PRIVATE); 
    if (rhymeDirectory.exists()) { 
     File individualRhyme = new File(rhymeDirectory, rhymeName); 
     if (individualRhyme.exists()) 
      return true; 
    } 
    return false; 
} 

這裏Constants.INTERNAL_DIRECTORY名是「韻」 我的理解是,如果沒有目錄,然後返回假,但是當我安裝我的應用程序第一次它返回true.Even我卸載它,然後重新安裝它總是返回true。我的問題是「爲什麼它總是返回true」?它不應該第一次返回錯誤嗎?如果我錯了,請糾正我。

回答

1

ContextWrapper.getDir()創建目錄,如果必要的,因爲在documentation說:

public File getDir (String name, int mode)

檢索,創建如果需要的話,一個新的目錄中,應用程序可以將自己的定製數據文件。

+0

如果該目錄已經存在將它創建新的或將取代它(可能是一個愚蠢的問題,但我有不知道這個)。 –

+0

如果您閱讀了文檔,該文檔會非常清楚:如果該目錄不存在,則會創建它。如果它存在,則返回現有的目錄。 – 1615903

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

    //Creating an internal dir; 
    File fileWithinMyDir = new File(mydir, "myfile"); 
    //Getting a file within the dir. 
    FileOutputStream out = new FileOutputStream(fileWithinMyDir); 
    //Use the stream as usual to write into the file 

刪除內部文件:??

if (new File("path/to/file").delete()) { 
    // Deleted 
    } else { 
    // Not deleted 
    } 
相關問題