Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep files after uninstallation of android app

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!

like image 518
Diego Avatar asked Mar 15 '12 19:03

Diego


1 Answers

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.

like image 90
CommonsWare Avatar answered Nov 15 '22 17:11

CommonsWare