10

I want to use a file to store purchased data. Since the app is going to be free and a certain amount of credits are given at the installation. What happens now is that if I uninstall the installation, also the files on the SD card are deleted. So at reinstallation you have your free credits again.

I use the following code:

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();
                }

So the file is saved in this directory getExternalFilesDir(null), apparently that directory is cleaned at uninstallation, anybody any tips for this matter?

Thanks!

Diego
  • 4,011
  • 10
  • 50
  • 76
  • Hm...I used more or less the same code, and had problems with the files BEING KEPT instead of getting deleted... – Eugen Mar 15 '12 at 19:44
  • According to the accpeted answer I seems like the only way to make sure your users don't "outsmart" you, is to use a server and accounts. Probably with saving of the IP adress. – mathematics-and-caffeine Apr 17 '20 at 10:16

1 Answers1

11

You can put files in a directory derived from Environment.getExternalStorageDirectory(). These files will persist after an uninstall. However, a user can delete those files any time they like, regardless of whether your app is installed or not.

Other than that, there is no place on the device that you can place files that will survive an uninstall.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Are you sure those files persist after an uninstall? I think you are mistaken. – Vlad Sep 29 '13 at 15:09
  • 2
    @Vlad: Files in `getExternalFilesDir()` and `getExternalCacheDir()` are removed upon uninstall. Files located elsewhere on external storage remain after an uninstall. – CommonsWare Sep 29 '13 at 15:29
  • are the files saved with `getExternalStorageDirectory()` accessible by user or do you need a root access to delete the data? – micromanc3r Nov 04 '14 at 19:49
  • 2
    Sadly, this method was deprecated in API level 29. Refer [here](https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()) – Ashish Emmanuel Apr 18 '20 at 03:44
  • @AshishEmmanuel Have you found alternative solution since Environment.getExternalStorageDirectory() was deprecated in API 29? – jianinz Jun 18 '20 at 15:48
  • But this method is deprecated. How to do with not-deprecated method? – Dims Jul 08 '20 at 13:32
  • @CommonsWare I think there is a flag on Android 10 for letting the user to choose "android:hasFragileUserData" . It is unchecked by default though, so the user has to check it to avoid the data from being removed. No idea what's the use of this. Seems weird. – android developer Feb 13 '21 at 13:11
  • @androiddeveloper: "No idea what's the use of this. Seems weird." -- agreed. – CommonsWare Feb 13 '21 at 13:21
  • There seems to be a way to still make this work. Read this: https://stackoverflow.com/questions/63842070/accessing-external-storage-in-android-api-29/63842348#63842348 – Edw590 Oct 02 '21 at 12:08