Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do temporary files get deleted even while the app is running?

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?

like image 903
android developer Avatar asked Nov 13 '22 00:11

android developer


1 Answers

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.

like image 184
nurisezgin Avatar answered Nov 15 '22 13:11

nurisezgin