I am developing an Android app which creates a folder in SD Card and stores some images. I want to remove that folder when the app is Uninstalled. Please guide me.
-
possible duplicate of [how to delete a file's on uninstall the android application?](http://stackoverflow.com/questions/4977351/how-to-delete-a-files-on-uninstall-the-android-application) – CodeCaster Nov 16 '11 at 14:17
3 Answers
Simple: Not possible.
There is currently no uninstall-event that get's triggered when your own app gets uninstalled. Hence you can't react to that in any way.
The only exception: Store your data in the folders provided by Context.getExternalFilesDir()
or Context.getExternalCacheDir()
. These will get deleted when your app gets uninstalled.
on API level 8 or greater use the external cache directory: http://developer.android.com/guide/topics/data/data-storage.html#ExternalCache
There is also an explanation for using API level 7 and lower in the above link

- 26,141
- 19
- 95
- 113
//for that you need to run the BroadcasrReciver and include the receiver in your androidmanifest.xml file
<receiver android:name="com.android.mobileasap.PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
// add permission
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
// in that PackageChangeReceiver just delete the file I am deleting the doc file in this below code
public class PackageChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//this.context=context;
Uri data = intent.getData();
Log.d("hi", "Action: " + intent.getAction());
Log.d("hi", "The DATA: " + data);
String action=intent.getAction();
if(Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(action))
{
String PATH = Environment.getExternalStorageDirectory() + "/mycontent_download/";
File file = new File(PATH);
if (file.exists())
{
String listOfFiles [] = file.list();
if (listOfFiles!=null)
{
if (listOfFiles.length>0)
{
int size = listOfFiles.length;
for (int i=0; i<size; i++)
{
if (listOfFiles[i].substring(listOfFiles[i].length()-4, listOfFiles[i].length()).equalsIgnoreCase(".doc"))
{
File f1 = new File(PATH+listOfFiles[i]);
f1.delete();
}
}
}
}
}

- 19,893
- 17
- 73
- 130