I would like to know how to create a file in the internal storage only during the .apk installing.
My problem is that when I put the file with the onCreate method of MainActivity, everytime I relaunch the application, the content of my file is deleted.
I also tried to use file.exists but it didn't help.
Here is the code I'm using :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Internal internal = new Internal();
String file_name = "profil1.txt";
String file_name1 = "profil2.txt";
File f = new File(file_name);
try {
if(f.exists()){
Log.d(TAG, "les fichiers sont deja cree");
}else {
FileOutputStream fos = openFileOutput(file_name, Context.MODE_WORLD_WRITEABLE);
FileOutputStream fos1 = openFileOutput(file_name1, Context.MODE_WORLD_WRITEABLE);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file =getFilesDir();
Log.d("TAG", file.getAbsolutePath());
Path = file.getAbsolutePath();
//internal.setFile();
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
setContentView(R.layout.main_grid);
mGrid = (GridView) findViewById(R.id.gridview);
mGrid.setColumnWidth(95);
mGrid.setVisibility(0x00000000);
// content.dispatchSystemUiVisibilityChanged(BIND_ADJUST_WITH_ACTIVITY);
registerIntentReceivers();
// requestWindowFeature(Window.FEATURE_NO_TITLE);
appManager = new ApplicationManager(this, getPackageManager());
appManager.loadApplications(true);
bindApplications();
bindButtons();
// Try to find activity to auto-start on create.
int i = 0;
while (i < APPLICATION_START_NAMES.length) {
Intent startIntent = appManager
.getIntentByName(APPLICATION_START_NAMES[i]);
if (startIntent != null) {
// Found it, let's start and continue.
startActivity(startIntent);
break;
}
i++;
Log.d("TAG", "application number" + i);
// we will try to hid the stat bar temporory because to hid it w e
// neeed root acces
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// Administrat.SINGLETON.init(this);
}
You can't execute code during installation. The code you've got in onCreate()
is almost right. The problem is that when you check the existence of the file, you haven't said where to look for it. You need to specify the application's private directory. Try this:
String file_name = "profil1.txt";
String file_name1 = "profil2.txt";
File f = new File(getFilesDir(), file_name); // File in the private directory
try {
if(f.exists()){
...
}
}