2012-03-15 41 views
3

我想使用文件来存储购买的数据。由于该应用将免费,并在安装时获得一定的积分。 现在发生的情况是,如果我卸载安装,SD卡上的文件也会被删除。因此,在重新安装时,您可以再次获得免费积分。卸载安卓应用程序后保留文件

我使用下面的代码:

File file = new File(mContext.getExternalFilesDir(null), FILENAME); 

       try { 

        FileOutputStream os = new FileOutputStream(file); 
        DataOutputStream out = new DataOutputStream(os); 


        out.writeInt(5); //5 CREDITS TO START 
        out.close(); 

        if(CreditFileExists()) { 
         Log.w("ExternalStorageFileCreation", "First Time so give 5 credits"); 
        } else { 
         Log.e("ExternalStorageFileCreation", "Error in creating CreditFile"); 
         Toast.makeText(mContext, R.string.sdnotavailable, Toast.LENGTH_SHORT).show(); 
        } 

       } catch (IOException e) { 
        // Unable to create file, likely because external storage is 
        // not currently mounted. 
        Log.e("ExternalStorage", "Error writing " + file, e); 
        Toast.makeText(mContext, R.string.sdnotavailable, Toast.LENGTH_SHORT).show(); 
       } 

所以文件保存在这个目录getExternalFilesDir(空),显然这个目录是在卸载干净,任何人对此事的任何提示?

谢谢!

+0

嗯......我用了或多或少相同的代码,并且遇到了文件BEING KEPT而不是被删除的问题...... – Eugen 2012-03-15 19:44:42

回答

6

您可以将文件放在从Environment.getExternalStorageDirectory()派生的目录中。这些文件将在卸载后保留。但是,无论您的应用程序是否安装,用户都可以随时删除这些文件。

除此之外,设备上没有可以放置文件的地方,这些文件将在卸载后仍然存在。

+0

您确定这些文件在卸载后仍然存在吗?我认为你错了。 – Vlad 2013-09-29 15:09:40

+1

@Vlad:卸载时会删除'getExternalFilesDir()'和'getExternalCacheDir()'中的文件。卸载后,位于外部存储器上其他位置的文件仍保留。 – CommonsWare 2013-09-29 15:29:45

+0

够公平的。抱歉。 – Vlad 2013-09-30 14:51:08

相关问题