2013-03-11 88 views
0

我想知道如何在.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); 
   } 
+0

你可以使用首選項來跟蹤文件是否應該被覆蓋。但就目前而言,您需要發佈一些代碼才能獲得實際幫助。 – DigCamara 2013-03-11 14:04:10

+0

@DigCamara我添加了我正在使用的代碼: – 2013-03-11 14:15:53

回答

1

安裝過程中不能執行代碼。您在onCreate()中獲得的代碼幾乎是正確的。問題是,當你檢查文件的存在時,你沒有說過去哪裏尋找它。您需要指定應用程序的專用目錄。試試這個:

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()){ 
      ... 
     } 
    } 
+0

另外:他的代碼並不完全清楚他如何使用這些文件(因爲它代表,如果文件存在,他會打開它們以及其他任何東西)。我懷疑變量聲明的範圍也是錯誤的,但我需要看到更多的代碼才能確定。 – DigCamara 2013-03-11 15:38:42

+0

它的工作!謝謝@DavidWasser。 – 2013-03-11 15:43:00

相關問題