Search code examples
androidweka

How to use getClassLoader() correctly?


Here I try to use getClassLoader().getResources() to get my .model file, however it returns null. I'm not sure where goes wrong!

And when I try to print out the urls, it gives me java.lang.TwoEnumerationsInOne@5fd1900, what does this means?

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();
    }
}

Solution

  • In Android, resources put under src/main/res are not visible in the class path and can only be accessed via the android resources API. Try to put the file into src/main/resources.