如何正确使用getClassLoader()?

时间:2016-04-09 16:56:25

标签: android weka

这里我尝试使用getClassLoader()。getResources()来获取我的.model文件,但是它返回null。我不确定哪里出了问题!

当我尝试打印网址时,它会给我 java.lang.TwoEnumerationsInOne@5fd1900 ,这意味着什么?

public Activity(MainActivity activity) {

      MainActivity activity = new MainActivity();

      try {
        // Open stream to read trained model from file
        InputStream is = null;

        // this .model file is save under my /project/app/src/main/res/
        Enumeration<URL> urls = Activity.class.getClassLoader().getResources("file.model");

        // System.out.println("url:"+urls);

        if (urls.hasMoreElements()) {
            URL element = urls.nextElement();
            is = element.openStream();
        }

        // deserialize the model
        classifier = (J48) SerializationHelper.read(is);
        is.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

在Android中,src/main/res下的资源在类路径中不可见,只能通过android资源API访问。尝试将文件放入src/main/resources