Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Application specific directory on SDCard that should be deleted when the app is uninstalled

Tags:

android

I want to use the device's SD Card for storing my application files, images, cache files etc on the device and use them in my application. I know that I can save them on the internal memory by using Context.getFilesDir() / Context.openFileOutput methods but I do not want my application to use internal memory for saving this data.

So is there any way I can create/save all my application specific files in a directory on the SD Card, but still be able to let the system know that these files have to be deleted when the application is uninstalled?

Or what are the recommendations in this case.

EDIT: I want to support 2.1 also.

Thank you.

like image 836
achie Avatar asked Jun 14 '11 17:06

achie


People also ask

Which of the following allows the user to delete a file which is saved in the internal storage area?

myFile. delete() ; If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():

Where are app files stored on Android?

For normal apps, there are stored in internal memory in /data/app. Some of the encrypted apps, the files are stored in /data/app-private. For apps stored in the external memory, files are stored in /mnt/sdcard/Android/data.


3 Answers

Your application doesn't get notified when it is being deleted, so you might be better off using the application's private directory, (from getFilesDir()). That directory is automatically deleted when the app is uninstalled.

Keep in mind that if you are on SDK 2.2+, you can allow your users the option of moving the entire application to the SD card, so you won't have to worry about the size of this directory.

like image 71
plowman Avatar answered Nov 15 '22 01:11

plowman


Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Link might help.

like image 29
Gopal Avatar answered Nov 15 '22 00:11

Gopal


This is a bit of a hack but it works for me...

File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/<package name>/files");
myFilesDir.mkdirs();

Replace '<package name>' with your package name.

EDIT: Thinking about it, it's possible it may only be 'cleaned up' on app un-installation on versions of Android from v2.2 onwards.

like image 30
Squonk Avatar answered Nov 14 '22 23:11

Squonk