Where to put your own properties file in the android project created in Android Studio?

I just started using Android Studio. I do not know where to place my own properties file, since there is no resource folder in the project structure.

The published snippet works fine in eclipse, but it does not work in Android Studio.

The code:

Properties prop = new Properties(); try { //load a properties file prop.load(new FileInputStream("app.properties")); //get the property value and print it out System.out.println(prop.getProperty("server_address")); } catch (IOException ex) { ex.printStackTrace(); } 

Question 1: Where to place my own properties files in Android Studio (v0.5.8)?

Question 2: How can I access them?

+8
android android-studio
source share
3 answers

By default, the resource folder is placed in src/main/assets , if it does not exist, create it.

Then you can access the file using something like this:

 getBaseContext().getAssets().open("app.properties") 

Read more about the Gradle structure of the Android project here .

+8
source share

Create a subfolder in the main folder and name its assets. Put all your .properties files in this folder (assets).

src-> Main-> assets-> mydetails.properties

You can access it using the AssetManager class.

 public class MainActivity extends ActionBarActivity { TextView textView; private PropertyReader propertyReader; private Context context; private Properties properties; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context=this; propertyReader = new PropertyReader(context); properties = propertyReader.getMyProperties("mydetails.properties"); textView = (TextView)findViewById(R.id.text); textView.setText(properties.getProperty("Name")); Toast.makeText(context, properties.getProperty("Designation"), Toast.LENGTH_LONG).show(); } 

}

 public class PropertyReader { private Context context; private Properties properties; public PropertyReader(Context context){ this.context=context; properties = new Properties(); } public Properties getMyProperties(String file){ try{ AssetManager assetManager = context.getAssets(); InputStream inputStream = assetManager.open(file); properties.load(inputStream); }catch (Exception e){ System.out.print(e.getMessage()); } return properties; } 

}

+5
source share

You can create your subfolder of your asset in your main folder, and then paste the .properties file into it.

Then create a class to open and read the file, for example:

 public class PropertiesReader { private Context context; private Properties properties; public PropertiesReader(Context context) { this.context = context; //creates a new object 'Properties' properties = new Properties(); public Properties getProperties(String FileName) { try { //access to the folder 'assets' AssetManager am = context.getAssets(); //opening the file InputStream inputStream = am.open(FileName); //loading of the properties properties.load(inputStream); } catch (IOException e) { Log.e("PropertiesReader",e.toString()); } } return properties; } } 

For more information see http://pillsfromtheweb.blogspot.it/2014/09/properties-file-in-android.html

0
source share

All Articles