I want to use a properties file to read out some config data in my android app. When I place the properties file within the class folder of the handler everything works fine, but I want to place the properties file in the project root. I already tried it with relative paths (../../../ etc.), but it didn't work.
Is there an "easy" way to do this (/config.properties also didn't work)?
Here is my code:
private Properties prop;
public PropertiesHandler() {
this.prop = new Properties();
InputStream input = null;
try {
input = getClass().getResourceAsStream("config.properties");
//input = new FileInputStream("config.properties");
prop.load(input);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Any help would be greatly appreciated.
EDIT:
Unfortunately I got this Exception when applying the suggested solution:
10-08 15:10:05.903: E/AndroidRuntime(967): FATAL EXCEPTION: main
10-08 15:10:05.903: E/AndroidRuntime(967): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.os.Looper.loop(Looper.java:137)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 15:10:05.903: E/AndroidRuntime(967): at dalvik.system.NativeStart.main(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): Caused by: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.dev.app1234.Register.<init>(Register.java:68)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.Class.newInstanceImpl(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.Class.newInstance(Class.java:1319)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 15:10:05.903: E/AndroidRuntime(967): ... 11 more
Here is my code:
public PropertiesHandler(Context context) {
this.prop = new Properties();
InputStream input = null;
try {
input = context.getAssets().open("config.properties");
And I call it like this:
private final String URL = new PropertiesHandler(this.getApplicationContext()).getUrl()
+ "url.php";
EDIT 2:
Code:
private final String URL = new PropertiesHandler(Register.this).getUrl()
+ "url.php";
Stack Trace:
10-08 20:45:14.421: E/AndroidRuntime(1024): FATAL EXCEPTION: main
10-08 20:45:14.421: E/AndroidRuntime(1024): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.os.Looper.loop(Looper.java:137)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 20:45:14.421: E/AndroidRuntime(1024): at dalvik.system.NativeStart.main(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): Caused by: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.model.PropertiesHandler.<init>(PropertiesHandler.java:25)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.Register.<init>(Register.java:68)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.Class.newInstanceImpl(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.Class.newInstance(Class.java:1319)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 20:45:14.421: E/AndroidRuntime(1024): ... 11 more
public class Register extends Activity