我想检查 /assets/ 文件夹中是否存在文件。
我怎样才能做到呢?请帮忙。
我向我的应用程序类之一添加了一个辅助方法。我假设;
- 应用程序运行时,资产列表不会更改。
- the
List<String>
不占用内存(我的应用程序中只有 78 个资源)。
- 检查列表上的 contains() 比尝试打开文件并处理异常更快(我实际上没有对此进行分析)。
AssetManager am;
List<String> mapList;
/**
* Checks if an asset exists.
*
* @param assetName
* @return boolean - true if there is an asset with that name.
*/
public boolean checkIfInAssets(String assetName) {
if (mapList == null) {
am = getAssets();
try {
mapList = Arrays.asList(am.list(""));
} catch (IOException e) {
}
}
return mapList.contains(assetName);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)