我正在将html文件作为"file:///android_asset/WebApplication/index.html"传递。如何在安卓中检查此文件是否存在。
您可以尝试打开流,如果它失败,则文件不存在,如果它没有失败,则文件应该在那里:
/**
* Check if an asset exists. This will fail if the asset has a size < 1 byte.
* @param context
* @param path
* @return TRUE if the asset exists and FALSE otherwise
*/
public static boolean assetExists(Context context, String path) {
boolean bAssetOk = false;
try {
InputStream stream = context.getAssets().open(ASSET_BASE_PATH + path);
stream.close();
bAssetOk = true;
} catch (FileNotFoundException e) {
Log.w("IOUtilities", "assetExists failed: "+e.toString());
} catch (IOException e) {
Log.w("IOUtilities", "assetExists failed: "+e.toString());
}
return bAssetOk;
}
[安卓资产]:文件还是文件夹?
如何从资产文件夹中获取所有文件
File f=new File("file:///android_asset/");
File[] files=f.listFiles();
如果文件返回空书房,您就知道文件夹是空的。