How to delete cache-folder of app?

前端 未结 5 558
刺人心
刺人心 2020-12-14 19:48

I read through the Android documentation of the cache (see Data Storage Documentation) but I didn\'t got how I can clean the whole folder.

So how can I dele

5条回答
  •  -上瘾入骨i
    2020-12-14 20:28

    Rather than rolling your own utility methods, you may want to consider using the apache commons FileUtils library. It contains a lot of useful File manipulation methods and makes operations like this very trivial.

    Here are the JavaDocs

    And here is an example:

    try {
        FileUtils.deleteDirectory(context.getCacheDir());
    } catch (IOException e) {
        Log.e(LOGTAG,"Error deleting cache dir", e);
    }
    

    Alternately, rather than deleting the whole cache directory, you may want to create subdirectories within the app's cache directory for specific data. Than you can delete those specific directories when required (e.g. on user logout).

提交回复
热议问题