Do temporary files get deleted even while the app

2019-06-21 08:35发布

问题:

Background

On android, you can create temporary files as such (link here and here for documentation) :

final File temp = File.createTempFile("myFile", ".tmp", context.getCacheDir());

I'm developing an app that uses temporary files and when it's done with them (and on start of the app), it deletes them.

The question

The documentation says :

When the device is low on internal storage space, Android may delete these cache files to recover space.

Does it mean that files can get deleted even while my app is still running? In which cases would android be allowed to delete the files?

In other words , can I assume that as long as I have the app running , android won't delete the files by itself, even if other apps create their own temporary files?

回答1:

You will can test it. Create new app, app write data to file and than internal stroge full. When internal stroge full Android delete applications cache directory which app store above 1Mb. If applications cache dir trashed, Android deleted even while the app is running. Otherwise while app running Android never deleted cache files.

This is the original link shared by developer.android.com http://developer.android.com/reference/android/content/Context.html#getCacheDir()

Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.

Truth

Why same people down vote me, i can't understand, i said truth information about internal cache warning.