小智 13
我在其中一个应用程序类中添加了一个辅助方法.我假设那个;
List<String>不是记忆力(我的应用程序中只有78个资产).Run Code Online (Sandbox Code Playgroud)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); }
您也可以尝试打开流,如果失败,文件就不存在,如果没有失败,文件应该在那里:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7679 次 |
| 最近记录: |