AssetManager无法在assets目录中找到文件

时间:2013-07-16 18:27:10

标签: java android eclipse

尝试从assets目录加载文件,以便将其用作配置文件。 AssetManager找不到它。 config.global和config.properties都位于项目的assets目录中。 config类在setUp()函数中生成,然后调用readConfig开始。这是主文件中的代码:

protected void setUp() throws Exception {
    super.setUp();
    mLithium = (LithiumExtension) Trio.getInstance(new LithiumExtension(this))
            .getExtension();
    config = StorefrontConfig.getInstance();
    Resources resources = getActivity().getResources();
    AssetManager asM = resources.getAssets();
    InputStream in = asM.open("config.global");
    OutputStream out = asM.openFd("config.properties").createOutputStream();
    config.readConfig(in, out);
    setTestSettings();
}

Config的代码:

public class Config {

private static Config instance;
public Properties prop;

static {
    instance = new Config();
}

private Config () {
    prop = new Properties();
}

public void readConfig(InputStream in, OutputStream out) {

    try {

        prop.load(in);
        in.close();

        prop.store(out, "yay");
        out.close();
    }
    catch (Exception e) {
        System.out.println("Failed to create properties");
        System.exit(3);
        Log.d("Config", "Failed to create properly initialized config class");
    }
}

public static Config getInstance() {

    if(instance == null)
        instance = new Config();
    return instance;
}

public String getProperty (String key) {
    return prop.getProperty(key);
}
}

运行时,java.io.FileNotFoundException获得config.global。这是在代码的asM.open("config.global")行引发的。被困在这一段时间了。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

setUp(Context c)内接收上下文并使用此功能获取资产

所以试试看 更改

 AssetManager asM = resources.getAssets();

AssetManager asM = c.getAssets();