2013-03-11 62 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

相关问题