This is what I typically do:
File outputDir = context.getCacheDir(); // context being the Activity pointer File outputFile = File.createTempFile("prefix", "extension", outputDir);
As for their deletion, I am not complete sure either. Since I use this in my implementation of a cache, I manually delete the oldest files till the cache directory size comes down to my preset value.
If you'd like to cache some data, rather than store it persistently, you should use
getCacheDir()to open a File that represents the internal directory where your application should save temporary cache files.
When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
To open a File that represents the external storage directory where you should save cache files, call
getExternalCacheDir(). If the user uninstalls your application, these files will be automatically deleted.
ContextCompat.getExternalFilesDirs(), mentioned above, you can also access a cache directory on a secondary external storage (if available) by calling
Tip: To preserve file space and maintain your app's performance, it's important that you carefully manage your cache files and remove those that aren't needed anymore throughout your app's lifecycle.