【发布时间】:2014-11-09 20:54:54
【问题描述】:
我目前在纯 Java 程序中使用 ResourceBundles。现在我想在 Android 应用程序中使用相同的文件。它按预期工作,当放置文件时,例如在/sdcard/。有没有办法直接从已知的 android 文件夹结构中加载它们?例如从没有手动将它们处理到 sdcard 的资产?
这适用于 sdcard:
File file = new File("/sdcard/");
URL[] urls = new URL[0];
try {
urls = new URL[]{file.toURI().toURL()};
} catch (MalformedURLException e) {
e.printStackTrace();
}
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("test", Locale.DEFAULT, loader);
此网址不适用于assets 文件夹:
urls = new URL[]{new URL("file:///android_asset/test/")};
感谢您的帮助。
【问题讨论】:
-
如果您想要一种从资产加载文件的方法,请查看此答案 [1]:stackoverflow.com/a/9544781/4224337
-
@Rami ResourceBunlde 无法处理 InputStream
标签: android assets resourcebundle