java 如何在Android的缓存目录中存储文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1628677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store files in Android's cache directory?
提问by Jacques René Mesrine
My goal is store temporary files in the directory returned by Context#getCacheDir().
我的目标是在 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.
我想我可以只使用 java.io apis 在那里写文件。但我不确定如何在那里设置文件权限,因为我无法使用 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:
Context#openFileOutput 和 Context#openFileInput 的文件名参数不能接受带有路径分隔符的输入。因此它们只对在目录中创建的文件有用:
/data/data/my.app
采纳答案by Kevin Tighe
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:
默认情况下,Android 中的每个应用程序都在自己的用户帐户下运行,因此任何其他应用程序都无法访问您的应用程序创建的文件。查看本页顶部的第 3 点:
http://developer.android.com/guide/topics/fundamentals.html
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.
您还可以通过在模拟器/设备中运行 shell 并查看 ls -l 的输出来验证这一点。
回答by CommonsWare
But I'm not sure how to set permissions on files there because I would not be able to use Context#MODE_PRIVATE.
但我不确定如何在那里设置文件权限,因为我无法使用 Context#MODE_PRIVATE。
AFAIK, they should be private by default.
AFAIK,默认情况下它们应该是私有的。

