开发者

android: Check the existence of a file in the application

I 开发者_开发技巧have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.

Following code will help you to understand my problem:

 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);

[it loads the newfile.html but not actualfilename.html]


You can't use File for resources. You'll need to use the AssetManager for that.

(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)


Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().

Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜