How to store files in Android's cache directory?

情到浓时终转凉″ 提交于 2019-12-23 08:40:08

问题


My goal is store temporary files in the directory returned by Context#getCacheDir().

/data/data/my.app/cache/somedir/foo.json

I presume I can just use java.io apis to write files there. But I'm not sure how to set permissions on files there because I would not be able to use Context#MODE_PRIVATE.

The filename parameter to Context#openFileOutput and Context#openFileInput cannot accept input with path delimiters. Thus they are only useful for files created in directory:

/data/data/my.app

回答1:


Each application in Android runs under it's own user account by default, so files created by your app would not be accessible from any other app. Check out bullet point #3 at the top of this page:

http://developer.android.com/guide/topics/fundamentals.html

You could also verify this by running a shell in your emulator/device and looking at the output of ls -l.




回答2:


But I'm not sure how to set permissions on files there because I would not be able to use Context#MODE_PRIVATE.

AFAIK, they should be private by default.



来源:https://stackoverflow.com/questions/1628677/how-to-store-files-in-androids-cache-directory

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