[英]android: Check the existence of a file in the application
我的應用程序的assets文件夾中有幾個html文件。 我的應用程序根據設備語言加載這些文件。 當我檢查文件是否存在時,它說不存在,但是當我使用browser.loadUrl(filename)加載該文件時,它會加載它。
以下代碼將幫助您了解我的問題:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[它加載newfile.html但不加載actualfilename.html]
您不能將File
用於資源。 您需要使用AssetManager
。
(在File
確實處理資源的情況下,我認為不會這樣做,你必須首先將路徑轉換為URI
,例如使用URI.create()
。文件(String)需要一個路徑,而不是URI。)
這是您使用的確切代碼嗎? 你可能想要調用f.exists()而不是filename.exist()。
編輯:嘗試使用AssetManager而不是硬編碼文件路徑。 我最好的猜測是你使用的文件路徑並不完全是它應該的樣子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.