Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can android clear files from the cache directory of my application while it is running?

I am implementing an image file cache in my application to reduce the number of times these files are downloaded directly from the web server. While I know the Android OS may decide to clear files from the cache directory of my application to free up internal storage space, I'm not quite sure if this "cleanup" process can happen even during its execution. If I could assume the OS won't mess with the cached files during execution, I would be able to use "in memory" data structures to optimize my cache implementation. For example, I could use a map (in memory) of cached entries (files stored on the cache folder) containing time stamps of cache hits and other information that might be useful to prioritize the way cached entries are recycled. Otherwise, if I have to deal with the fact that the cache cleanup process may happen anytime, anywhere, even during app execution, use of in memory data structures would be useless because they would eventually become out of sync with the real contents of the folder in the event of the OS deciding to clean up files by itself. In which case my cache system would have to be less efficient because it would be forced to keep checking the contents of the cache folder directly in the file system everytime a new file is requested by the app. In other words, I just want to know if I can assume the contents of the cache directory wont be changed by the OS concurrently with my app because that might considerably change my cache implementation.

like image 658
ulix Avatar asked Nov 18 '14 04:11

ulix


1 Answers

just delete all files from the cache directory with normal method

public void clear() {
    File[] directory = getCacheDir().listFiles();
    if(directory != null){
        for (File file : directory ){
            file.delete();
        }
     }
 }
like image 195
Sam Avatar answered Nov 08 '22 08:11

Sam