Keep files after uninstallation of android app

2019-06-27 13: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!

回答1:

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.