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
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:
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