Include a data file with my Android app
I'm c开发者_运维知识库reating an app that I want to seed with a data file the app will use as its initial state. In the Eclipse project structure, where do I add the data file so that when the app is deployed to the device (and emulator) the data file is deployed with it?
It will be helpful it you can the data file type and the purpose. If you are looking to initalize certain settings you can do it through:
Describe preferences in xml --- store in RES/XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory >
<CheckBoxPreference />
</PreferenceCategory>
</PreferenceScreen>
Load for sure onCreate():
/* Loading default preferences the first time application is run */
PreferenceManager.setDefaultValues(getApplicationContext(),
R.xml.generalsettings, false);
Else,
You can hardcode data and store in the db:
String sql = "CREATE TABLE ..." + "INSERT VALUES ... INTO ... ";
db.ExecSQL(sql);
Else you can store this file in the Assets folder and use getAssets() to retrieve the file:
getAssets().open(fileName)
Eg. using raw data file. In case of assets replace with above command:
InputStream is = context.getResources().openRawResource(R.raw.artoodict);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
while ((readLine = br.readLine()) != null) {
}
}
} catch (IOException e) {
e.printStackTrace();
}
精彩评论