Android - Preserve or delete files created by the application on uninstall
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.
How delete files and folders when the App is uninstalled?
Well, that doesn't seem to be achievable as we dont get any callback information regarding uninstall.
what i have tried in my career :
BroadCast Receiver : wont work as once app is uninstalled it doesn't exist anymore on OS therefore wont be able to get broadcast.
Therefore theirs no way to remove folders created on device's external storage.
What you can do :
Use getExternalCacheDir(), then the folders created are auto deleted
when app is uninstalled.If your app target is API Level 8 or higher, you can use Context#getExternalFilesDir() for your external files and those will be removed on uninstall.
Keep files after uninstallation of android app
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.
Android - delete files on SD card on uninstall
Could you download the files and write them to the apps internal space using the methods explained here : Internal Storage
Files written to the internal storage are automatically removed when the user uninstalls your application.
From what I've read Android does not allow you to modify the uninstall process to help protect against malware.
Starting with Android 2.2 (API level 8) apps can be installed on the sdcard.
See here for more info : Installing to application to sd card
Edit : apparently there is also an external cache that you can use that, according to the doc gets removed when the user uninstalls your app also : External cache
When I uninstall my app , Can we remove my folder which is created through my app?
What should I do to remove it ?
Use getExternalFilesDir()
and/or getExternalCacheDir()
for your files on external storage ("sdcard"). Those directories are automatically removed when your app is uninstalled.
Beyond this, nothing else is possible, as you do not get control when your app is removed.
Android, how to prevent internal storage files to be deleted when the app is uninstalled
You can save those file using Environment.getExternalStorageDirectory()
This stores on the external storage device. Dont get confused with the term external storage as the SD card. SD card is the secondary external storage. But Environment.getExternalStorageDirectory()
returns top-level directory of the primary external storage of your device which is basically a non removable storage.
So the file path can be /storage/emulated/0/YOURFOLDER/my.xml
So even if you uninstall the app, these files will not get deleted.
You can use this snippet to create a file in your primary external storage:
private final String fileName = "note.txt";
private void writeFile() {
File extStore = Environment.getExternalStorageDirectory();
// ==> /storage/emulated/0/note.txt
String path = extStore.getAbsolutePath() + "/" + fileName;
Log.i("ExternalStorageDemo", "Save to: " + path);
String data = editText.getText().toString();
try {
File myFile = new File(path);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(data);
myOutWriter.close();
fOut.close();
Toast.makeText(getApplicationContext(), fileName + " saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
Don't forget to add below permission in Android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
You can then read that file as below:
private void readFile() {
File extStore = Environment.getExternalStorageDirectory();
// ==> /storage/emulated/0/note.txt
String path = extStore.getAbsolutePath() + "/" + fileName;
Log.i("ExternalStorageDemo", "Read file: " + path);
String s = "";
String fileContent = "";
try {
File myFile = new File(path);
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
while ((s = myReader.readLine()) != null) {
fileContent += s + "\n";
}
myReader.close();
this.textView.setText(fileContent);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), fileContent, Toast.LENGTH_LONG).show();
}
Related Topics
Dynamically Updating an Autocompletetextview Adapter
Adding Tab Inside Fragment in Android
How to Tell If a Closed Path Contains a Given Point
Assign Width to Half Available Screen Width Declaratively
Error:Unexpected Lock Protocol Found in Lock File. Expected 3, Found 0
Android Failed to Install Helloworld.Apk on Device (Null) Error
Getting Certificate Details from an APK
Android Webview, Scaling Image to Fit the Screen
Android Hello, Gallery Tutorial -- "R.Styleable Cannot Be Resolved"
Android Gridview Draw Dividers
Android Scrollview Layout Problem
Autocompletetextview Not Completing Words Inside Parentheses
How to Create Xmpp Chat Client for Facebook
Android Runtime Layout Tutorial