我想知道如何在.apk安裝期間在內部存儲器中創建一個文件。如何僅在安裝過程中在內部存儲器中創建文件?
我的問題是,當我用MainActivity的onCreate方法放置文件時,每當我重新啓動應用程序時,我的文件的內容都將被刪除。
我也嘗試使用file.exists,但它沒有幫助。
這裏是我使用的代碼:
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);
}
你可以使用首選項來跟蹤文件是否應該被覆蓋。但就目前而言,您需要發佈一些代碼才能獲得實際幫助。 – DigCamara 2013-03-11 14:04:10
@DigCamara我添加了我正在使用的代碼: – 2013-03-11 14:15:53