将自己的属性文件放在使用 Android Studio 创建的 android 项目中的何处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23792029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put own properties file in an android project created with Android Studio?
提问by bakcsa83
I just started to use Android Studio. I don't know where to put my own properties file since there is no assets folder in the project structure.
我刚开始使用 Android Studio。我不知道把我自己的属性文件放在哪里,因为项目结构中没有资产文件夹。
The posted snippet works fine in eclipse, but it doesn't in Android Studio.
发布的代码段在 eclipse 中运行良好,但在 Android Studio 中却没有。
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 put my own properties files in Android Studio (v0.5.8)?
问题 1:在 Android Studio (v0.5.8) 中,我自己的属性文件应该放在哪里?
Question 2:How can I access them?
问题 2:我如何访问它们?
采纳答案by Salem
By default assets folder is placed in src/main/assets
, if it does not exists create it.
默认情况下,assets 文件夹放置在 中src/main/assets
,如果它不存在,则创建它。
Then you can access the file using something like this:
然后您可以使用以下内容访问该文件:
getBaseContext().getAssets().open("app.properties")
You can find more info about Gradle Android project structure here.
您可以在此处找到有关 Gradle Android 项目结构的更多信息。
回答by Prashant_M
Create a subfolder in your main folder and name it assets. Place all your .properties files in this(assets) folder.
在主文件夹中创建一个子文件夹并将其命名为 assets。将所有 .properties 文件放在 this(assets) 文件夹中。
src->main->assets->mydetails.properties
src->main->assets->mydetails.properties
You can access it using AssetManager class
您可以使用 AssetManager 类访问它
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;
}
}
回答by SegFault
You can create your asset subfolder in your main folder and then insert the .properties file in it.
您可以在主文件夹中创建资产子文件夹,然后在其中插入 .properties 文件。
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
有关更多信息,请参阅http://pillsfromtheweb.blogspot.it/2014/09/properties-file-in-android.html
回答by Sushil Ranjan
public static String getProperty(String key, Context context) throws IOException {
try {
Properties properties = new Properties();
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("config.properties");
properties.load(inputStream);
return properties.getProperty(key);
}catch (IOException e){
e.fillInStackTrace();
}
return null;
}
Folder Structure:
文件夹结构: