getExternalCacheDir() returns null after clearing data

戏子无情 提交于 2019-12-03 04:53:55
Robert

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() .

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!