将公共文件保存在内部存储器中
我有一个保存从服务器下载的文件的应用程序。这些文件不是我的应用程序的私有文件,其他应用程序也应该可以访问。我想知道当 SD 卡不存在时保存这些文件的正确路径是什么。对于SD卡,有众所周知的API -
getExternalStorageDirectory()
对于内部存储器中的应用程序的私有数据,有 -
Context.getFilesDir()
在某些设备上,内部存储器由/emmc/表示。
如果有人能详细说明/emmc/,那将非常有帮助。我知道它代表嵌入式存储卡,并非所有设备中都存在。但它真的代表内存吗?还是第三个记忆?
我应该使用 openFileOutput()
和 MODE_WORLD_READABLE
保存文件吗?
I have an application that saves files downloaded from a server. These files are not private to my application and should be accessible to other applications as well. I want to know what would be the correct path to save these files when the SD card is ABSENT. For the SD card, there is the well known API -
getExternalStorageDirectory()
For the application's private data in the internal memory there is -
Context.getFilesDir()
On some devices, the internal memory is represented by /emmc/.
It will be really helpful if someone could elaborate on /emmc/. I know it stands for embedded Memory card and is not present in all the devices. But is it really representative of the internal memory? Or is it the third memory?
Should I save the files using openFileOutput()
with MODE_WORLD_READABLE
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这个问题已经过时并且有一个公认的答案,但我认为这个问题仍然相关,并且有一种方法可以实现提问者想要的东西。
换句话说,我相信询问者想要保存文件,并使其可由其他应用程序访问(例如查看),而不必考虑外部存储(例如额外的权限以及外部存储不存在的可能性)。
事实证明,您可以使自己的文件可供其他应用程序读取。
setReadable 使上面的代码对我有用 - 没有该行,查看器应用程序无法读取文件。请注意,setReadable 对整个目录进行操作,因此最好的解决方案可能是创建一个名为“public”的子目录并将文件放入其中。
I realize this is old and has an accepted answer, but I think the question is still relevant and that there is a way to achieve what the asker wants.
To paraphrase, I believe the asker wants to save a file, and make it accessible (e.g. for viewing) by other apps, without having to think about external storage (e.g. an additional permission and the possibility that the external storage is not present).
It turns out that you can make your own files readable by other apps.
The setReadable made the above code work for me - without that line the viewer app couldn't read the file. Note that setReadable operates on the whole directory, so the best solution is probably to create a sub-directory name 'public' and put your files in there.
/emmc/
可能存在于某些设备上,并且它可能是内部的,但应用程序可能无法访问它,并且它肯定不是 Android SDK 的一部分,除非它恰好是getExternalStorageDirectory() 返回。
不。
询问您的设备制造商。
这是不可能抽象地回答的。您说您的文件“也应该可供其他应用程序访问”,但您没有说明为什么您希望任何其他应用程序关心您的文件。其他应用程序不会扫描您的目录中的文件 - 充其量,它们将允许用户浏览外部存储。拥有
MODE_WORLD_READABLE
文件的唯一原因是您的应用程序是否会触发另一个应用程序对该文件执行某些操作(例如,ACTION_VIEW
意图
)。/emmc/
may exist on some devices, and it may be internal, but it may not be accessible to applications, and it certainly is not part of the Android SDK unless that happens to be whatgetExternalStorageDirectory()
returns.No.
Ask your device manufacturer.
That is impossible to answer in the abstract. You say that your files "should be accessible to other applications as well", but you have not indicated why you expect any other application to care one bit about your files. Other applications will not be scanning your directories for files -- at best, they will allow users to browse external storage. The only reason to have a file that is
MODE_WORLD_READABLE
is if your application will be triggering another application to do something with the file (e.g.,ACTION_VIEW
Intent
).