2012-02-14 40 views
3

我試圖讓現在幾個小時的getFileStreamPath瘋狂的感覺。 有人可以解釋如何測試路徑=「shop/crates/fruits」嗎? 爲了簡化測試,我已經打破了分段的路徑。 我以爲我擁有它。但是,當商店存在時,測試中斷,但沒有箱子..奇怪! 或者是?我無法理解Context.getFileStreamPath試圖測試文件路徑是否存在?

public static Boolean pathExists(String path, Context ctx) 
{ 
    Boolean result = false; 
    String[] pathSegments = path.split("/"); 
    String pathStr = ""; 
    for(int i = 0;i<pathSegments.length;i++) 
    { 
     pathStr += pathSegments[i]; 
     if(!ctx.getFileStreamPath(pathStr).exists()) 
     { 
      result = false; 
      break; 
     } 
     pathStr += "/"; 
     result = true; 
    } 
    return result; 
} 

回答

0

首先,getFileStreamPath返回上其中具有openFileOutput(字符串,整數)創建的文件存儲在文件系統中的絕對路徑。

您是否試圖測試內部或外部存儲的路徑?如果您想使用外部存儲,請使用getExternalFilesDir()。如果你想使用內部包嵌入資源,如res/raw,又是另一回事:

Android how to get access to raw resources that i put in res folder?

但我不認爲它會工作你假設得到它的方式。

仔細閱讀http://developer.android.com/guide/topics/data/data-storage.html

閱讀Using the Internal Storage章那裏。

另見:

How to create a file on Android Internal Storage?

Read/write file to internal private storage

http://www.vogella.de/articles/AndroidFileSystem/article.html

+1

_你似乎沒有得到它的工作原理。首先,getFileStreamPath返回文件系統上使用openFileOutput(String,int)創建的文件的絕對路徑._ 我完全同意。 使用文件和目錄總是有點難看。 Android文件系統比您的平均文件系統對象更醜。 當然,你必須RTFM,然後你會沒事的。除非你錯過了最後期限。 我知道我並不孤單,這個位置。有很多受挫的人試圖破解它。 太糟糕了。看起來像一個完整的重新設計給我。 – DevNull 2012-02-14 06:15:00

2

呀,糟糕的Android的API。 祕訣就是使用context.getFilesDir()。這是你的文件:

File file = new File(context.getFilesDir() + File.separator + path); 

之後,該文件的行爲正常。

相關問題