卸载android app后保留文件

Die*_*ego 5 installation file-io storage android

我想使用文件来存储购买的数据.由于该应用程序将是免费的,并且在安装时会给出一定数量的学分.现在发生的是,如果我卸载安装,也会删除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();
                }
Run Code Online (Sandbox Code Playgroud)

所以文件保存在这个目录中的getExternalFilesDir(null),显然在卸载时清理了目录,任何人都有这方面的提示吗?

谢谢!

Com*_*are 8

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

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

  • @Vlad:卸载时删除`getExternalFilesDir()` 和`getExternalCacheDir()` 中的文件。卸载后,位于外部存储其他位置的文件仍然存在。 (2认同)
  • 遗憾的是,此方法在 API 级别 29 中已被弃用。请参阅[此处](https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()) (2认同)