I created an application which creates and stores files to sdcard. Is there a way to bind the folder with application in order to delete all files when the user runs uninsta
Seems like there have been some developments since 2009 :).
From the documentation:
If you're using API Level 8 or greater, use
getExternalCacheDir()
to open a File that represents the external storage directory where you should save cache files. If the user uninstalls your application, these files will be automatically deleted. However, during the life of your application, you should manage these cache files and remove those that aren't needed in order to preserve file space.If you're using API Level 7 or lower, use
getExternalStorageDirectory()
to open a File that represents the root of the external storage, then write your cache data in the following directory:/Android/data//cache/ The is your Java-style package name, such as "com.example.android.app".
Even when using the getExternalCacheDir()
, I have seen that the cache directory, which is your app's package name, is not removed automatically as it says on the Android Documentation, at least not on the Lenovo A1. It could be a bit naive but you may want to clean up previous data during re-install. There, you can find out if your app was un-installed or is being installed for the first time - you store a value, perhaps a boolean, using shared preferences. It can be something like:
SharedPreferences sharedPrefs = getSharedPreferences(INSTALL_PREFS, MODE_PRIVATE);
if(sharedPrefs.getBoolean("AppInstalled", false) == false){
//DELETE APP DIRECTORY
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("AppInstalled", true);
editor.commit();
}
This way, everytime the app runs, it checks if it's the first time it's running. When the app is removed, so will the shared preferences for the app. Then, the next time it is installed, the if(...) will be true and you can do some clean up there.
I know this doesn't answer the question directly and it's not nice to leave unused data on users' devices, but I think it could be an alternative to trying to clean up when an app is being removed, which is sensible but seems impossible.
it seems since then there is a proper soltuion, Context.getExternalFilesDir():
Returns the absolute path to the directory on the external filesystem (that is somewhere on Environment.getExternalStorageDirectory()) where the application can place persistent files it owns. These files are private to the applications, and not typically visible to the user as media.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled
There's no way for your application to know that it is being uninstalled (without modifying the kernel). All files created in the data/data/your.app.package
is deleted automatically upon application uninstall.
I don't think anything is cleared from the sdcard. You can do a quick test and find that out.
Another approach could be to have another application that checks whether this application is installed or not. If not, it can do the clean-up work.
I'm adding this because the existing answers are outdated in 2017. While it is true that there is no callback on app uninstall, some mechanisms have been provided for preserving app data beyond uninstall.
Since Android 6.0 (API 23), the Auto Backup for Apps feature was introduced, which allowed developers to either permit or prevent automatic application backup. As of API 23 backup is enabled by default.
Two new application
tags have been introduced for the manifest file: android:allowBackup
and android:fullBackupContent
.
To opt out of automatic backup, add android:allowBackup="false"
to the manifest file under the application
tag. A value of "true" will automatically save shared preferences and databases to the cloud, as well as other files.
Secondly, you can be very specific about the files you want to include or exclude for deletion or preservation on uninstall with the android:fullBackupContent
tag. This attribute points to an XML file that contains backup rules. Create an XML configuration file in the res/xml/
directory with the following syntax:
<full-backup-content>
<include domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string" />
<exclude domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string" />
</full-backup-content>
Here you can add the specific file types to include or exclude and their associated paths. Add the android:fullBackupContent = "@xml/my_backup_rules"
tag to the manifest file under the application
tag.
The specified files would be backed up to Google Drive under the primary email account of the device. The data would be restored on re-install provided the following setting is enabled by the user:
Settings -> Backup & Reset -> Automatic Restore
.
It should also be noted that the backup data cannot be read by the user or other applications on the device.
The same can also be accessed from the Google Drive app from here:
Google Drive -> Settings -> Auto backup for apps -> Backup and reset
It is worth noting that if the same primary Google account is used for multiple devices, then multiple device backups are created. The backups for individual devices can be accessed from here:
Google Drive -> Settings -> Backups
if you want to store large data then store it in external storage with file path name such as
Environment.getExternalStorageDirectory().toString()+"/Android/data/packageName"
where package name is your app package name eg com.think.abc