getExternalCacheDir() returns null after clearing data

前端 未结 3 1264
忘了有多久
忘了有多久 2021-02-07 04:45

I have a simple app that access and writes data to external storage. Everything works fine until I go to Settings -> Apps -> App Info and clear data via \"Clear data\" button, t

相关标签:
3条回答
  • 2021-02-07 04:53

    getExternalCacheDir() returns cache dir like the name says. If there is no cache, there is no directory for it either. This directory is used for temporary files you removed with remove data command. In cases where phone is low on space, it can remove these folders itself too. Atleast some maintenance applications do so.

    getExternalFilesDir() returns the directory for space to save data.

    0 讨论(0)
  • 2021-02-07 04:53

    If you use getExternal*Cache*Dir(), it is in order to store TEMPORALY data that can be cleaned by the system. If other applications in front on the backstack needs resources, the systeme can clean your data because system need resources. if you want save your data persistently, use : File file = getExternalFilesDir(null); this is an external memory for storage persitent data (like a virtual sdcard).

    0 讨论(0)
  • 2021-02-07 05:03

    I have encountered and solved this exact problem.

    Clicking the Clear Data button causes Android to stop your app from running and delete the entire application-specific folder "mnt/sdcard/Android/data/your.package.name" .

    However, I had a separate process that was started from Runtime.getRuntime().exec() that was still running and it was writing to this folder. This caused the folder to be stuck in a locked state and caused the same symptom you described when my app called getExternalCacheDir() . Running adb shell and then ls from within the /mnt/sdcard/Android/data folder showed that the folder was locked by a process. Running ps showed that my other process was still running.

    The solution was to properly kill the other process that was still writing to my application's application-specific folder before calling getExternalCacheDir() .

    0 讨论(0)
提交回复
热议问题