3

I'm working on an Android application and I need to read my properties in assets folder in /app/src/main/assets/app.properties.

But when I use:

Properties properties = new Properties();
try {
properties.load(new FileInputStream("app.properties"));
} catch (IOException e) {
...
}

The inputStream seems to be null. I think I have to precise the filepath or something like that to access to my properties file.

I need to use my properties in this class: /app/src/main/java/mypackage/model/myclass.java

Zoe
  • 27,060
  • 21
  • 118
  • 148
Swodniw
  • 35
  • 1
  • 10

4 Answers4

2

You can load the propertiy file using your android context like this :

context.getAssets().open("app.properties");

For example in a Fragment :

try{
    Properties properties = new Properties();
    properties.load(this.getActivity().getAssets().open("app.properties"));
}catch(Exception e){
    e.printStackTrace();
}

As you seem to need this in a class where the context is not accessible you can create your own application class with a static access to the context and then use this context everywhere.

Create your application class :

public class MyApp extends Application {
    private static MyApp instance;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
        return instance.getApplicationContext()
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }
}

Add your new created application into the manifest :

<application
    android:name="com.example.yourapp.MyApp"
    ...

Once this done you can load your properties in your XMLParser :

try{
    Properties properties = new Properties();
    properties.load(MyApp.getContext().getAssets().open("app.properties"));
}catch(Exception e){
    e.printStackTrace();
}
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
  • I can't use getActivity().getAssets().... because i'm in a class which is not an Activity, And i can't use context. My class is an XMLParser and my properties file stores all XML nodes i can find in my XML file. – Swodniw Apr 13 '16 at 12:49
  • See my updated answer to acces the context into your parser. – Guillaume Barré Apr 13 '16 at 12:58
  • THANK YOU!!! it works, i didnt know that we can create a "virtual context" and that's exactly what i needed. – Swodniw Apr 13 '16 at 13:53
  • @Nirekin why not set `instance = this` in constructor of the application class? – Bhargav May 28 '17 at 15:26
1

Try getting the asset and them reading it:

AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
FileInputStream stream = fileDescriptor.createInputStream();
Properties properties = new Properties();
try {
properties.load(stream);
} catch (IOException e) {
...
}
Miguel Benitez
  • 2,322
  • 10
  • 22
0

In my unit tests "XMLParserTest" in /app/src/test/java/mypackage/XMLParserTest I tried with:

    InputStream is = null;
    Properties prop = null;
    try {
        prop = new Properties();
        is = new FileInputStream(new File("C:/...fullpath.../app/src/main/assets/app.properties"));
        prop.load(is);
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }

it works but when i put it in my XMLParser class my inputStream "is" is null. It is like the class can't access to the file .properties. It works for tests only...

Swodniw
  • 35
  • 1
  • 10
0

If you want to use a library: https://github.com/fernandodev/android-properties

Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58