android - 使用 Android 库从 Android 应用程序获取属性文件中的值

标签 android

从这里question ,我知道如何在 Android 应用程序中获取属性。

但是,如果我想编写一个 Android 库,并打算读取一个属性文件(比如 myconfig.properties,我该怎么做?

鉴于 Android 库(例如它称为“myAndroidLib”)具有 android-support-v4.jar ,并且该库被导入到 Android 应用程序中。还假设 myconfig.properties 存在。

我在网上找到了一段代码,但这只适用于Android:

Resources resources = this.getResources();
AssetManager assetManager = resources.getAssets();

// Read from the /assets directory
try {
    InputStream inputStream = assetManager.open("microlog.properties");
    Properties properties = new Properties();
    properties.load(inputStream);
    System.out.println("The properties are now loaded");
    System.out.println("properties: " + properties);
} catch (IOException e) {
    System.err.println("Failed to open microlog property file");
    e.printStackTrace();
}

最佳答案

I found a piece of code in the Internet, but this works only within Android

该代码将在 Android 库项目中正常运行。您将需要托管应用程序为您提供一个 Context,您最终可以从中派生出 AssetManager

关于android - 使用 Android 库从 Android 应用程序获取属性文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788985/

相关文章:

android - 如何从 Activity 和 BroadcastReceiver 重现应用程序无响应 (ANR)

java - LIBGDX,安卓 : deadlock killing app when when returning captured picture

android - 如何在android的listview中只显示两行数据

android - 有没有机会让键盘显示在标签图标上

android - Android 上的 Netty ObjectEncoders/ObjectDecoders 导致 StreamCorruptedException

Android EditText 错误消息弹出文本未显示

android - 自动化 Java 到 Kotlin 的转换 : unresolved reference to 'ARG_LAYOUT_RES_ID'

android - 获取从android中的图库中选择的图像的图像名称

android - nginx + SSL 证书在 Android 上无效

java - 从 android listView 中删除项目