我想检查 /assets/ 文件夹中是否存在文件。我怎么能做到?请帮忙。
			
			7817 次
		
4 回答
            13        
        
		
我向我的一个应用程序类添加了一个辅助方法。我假设;
- 应用程序运行时资产列表不会更改。
 - 这
List<String>不是内存猪(我的应用程序中只有 78 个资产)。 - 检查 List 上的 exists() 比尝试打开文件并处理异常要快(我实际上没有对此进行分析)。
 
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); }
于 2011-08-14T04:25:12.167   回答
    
    
            9        
        
		
您也可以尝试打开流,如果失败,则文件不存在,如果不失败,则文件应该在那里:
/**
 * 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;
}
    于 2011-11-09T19:10:47.890   回答
    
    
            4        
        
		
您可以使用 Resources.openRawResourceFd(int resId)
http://developer.android.com/reference/android/content/res/Resources.html#openRawResourceFd%28int%29
于 2010-11-11T10:25:08.627   回答
    
    
            3        
        
		
您必须执行自己的检查。据我所知,这项工作没有任何方法。
于 2010-12-13T08:44:46.007   回答