How to create a special application directory on the SDCard, which should be deleted when the application is deleted

I want to use the device’s SD card to store application files, images, file cache, etc. on the device and use them in your application. I know that I can save them in internal memory using the Context.getFilesDir () / Context.openFileOutput methods, but I do not want my application to use internal memory to store this data.

So, is it possible to somehow create / save all the files depending on my application in a directory on the SD card, but still be able to tell the system that these files should be deleted when the application is deleted?

Or what recommendations in this case.

EDIT: I want to support 2.1 as well.

Thanks.

+6
source share
3 answers

Your application does not receive notification of its removal, so you might be better off using the application’s private directory (from getFilesDir ()). This directory is automatically deleted when the application is uninstalled.

Keep in mind that if you use the SDK 2.2+, you can allow your users to move the entire application to the SD card, so you don’t have to worry about the size of this directory.

+6
source

Any files that are private to the application must be placed in the directory returned by Context.getExternalFilesDir, which the system will take care of removal if the application is deleted. Link can help.

+5
source

It's a little hack, but it works for me ...

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

Replace '<package name>' your package name.

EDIT: Thinking about this, it is possible that it may be "cleared" when the application is disabled in Android versions starting from version 2.2.

+3
source

Source: https://habr.com/ru/post/890528/


All Articles