将私有访问文件写入另一个应用程序的文件目录
这两个应用程序具有相同的sharedUserId。当我在 app1 中使用此代码时,
context.openFileOutput("/data/data/org.me.app2/files/shared-data.dat", MODE_PRIVATE)
出现异常,告诉我该文件包含路径分隔符。
我正在尝试将文件从 app1 写入到 app2 的存储中。 (我当然需要确保首先存在 app2 的文件目录)
理想情况下,我会写入用户特定目录而不是应用程序特定目录,但我不知道是否可以做到
The two apps have the same sharedUserId. When I use this code in app1
context.openFileOutput("/data/data/org.me.app2/files/shared-data.dat", MODE_PRIVATE)
I get an exception telling me that the file contains a path separator.
I am trying to write a file from app1 into app2's storage. (I do of course need to make sure that app2's files directory exists first)
Ideally, I would write to a user specific directory instead of an app specific directory, but I do not know if that can be done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,切勿使用内部存储的完整路径,例如
/data/data
。让操作系统为您提供路径(例如,通过Context.getFilesDir()
或Environment.getExternalStorageState()
)。不要假设数据在哪里。其次——你已经在这样做了!与
File
不同,Context.openFileOutput
已将/data/data/[package]
添加到您的路径中,因此您无需指定该路径。只需指定文件名即可。如果您确实认为这是安全且必要的,并且两个应用程序在清单中使用 android:sharedUserId 共享相同的用户 ID,则可以使用
Context.createPackageContext()
并使用CONTEXT_RESTRICTED,然后使用仅包含文件名的 openFileOutput。First of all, NEVER use a full path to internal storage like
/data/data
. Let the operating system give you the path (for example, viaContext.getFilesDir()
orEnvironment.getExternalStorageState()
). Don't make assumption on where the data is.Secondly - you already are doing that! Unlike
File
,Context.openFileOutput
already prepends/data/data/[package]
to your path, so you don't need to specify that. Just specify the file name.If you really feel that it's safe and necessary, and if both apps share the same user ID using android:sharedUserId in the manifest, you can get a context of the other app by using
Context.createPackageContext()
and use CONTEXT_RESTRICTED, then use openFileOutput with only the file name.打开所需文件的
FileOutputStream
,相对于此路径:Open a
FileOutputStream
of the needed file, relative to this path:由于这已经有几个月了,我认为您已经解决了您的问题,但无论如何我都会做出贡献。
ContentProvider 的用途是在应用程序之间共享数据。假设您知道如何编写 ContentProvider 并访问它,您可以通过 ParcelFileDescriptor 访问文件,其中包含创建文件的模式的常量。
您现在需要的是限制访问权限,以便不是每个人都可以通过内容提供商读取文件,并且您可以通过 android 权限来做到这一点。在您的应用程序(将托管文件和内容提供程序的应用程序)的清单中,编写如下内容:
并在两个应用程序中添加以下内容:
通过使用protectionLevel =“signature”,只有您签名的应用程序才能访问您的内容提供程序,以及您的文件。
Since this is months old I assume you've already solved your problem, but I'll contribute anyway.
Sharing data between apps is what ContentProviders are for. Assuming that you know how to write a ContentProvider and access it, you can access files via ParcelFileDescriptor, which includes constants for the mode in which you create the files.
What you need now is to limit access so that not everybody can read the files through the content provider, and you do that via android permissions. In the manifest of one your apps, the one that will host the files and the content provider, write something like this:
and in both apps add this:
by using protectionLevel="signature", only apps signed by you can access your content provider, and thus your files.
您不应该覆盖其他应用程序文件。也就是说,您有两种解决方案
编辑:开发人员拥有这两个应用程序
感谢 Roman Kurik 指出了这一点。从
android 文档
所以这正是用户 ID 在 Linux 中的工作方式,本质上你是两者的所有者并且对两者都有读/写访问权限。
You should not be overwriting other applications files. That said you have two solutions
Edit: Developer owns both applications
Thanks for Roman Kurik for pointing this out. A link to his post on SO
From the android docs
So this is exactly the way user id's work in linux, essentially you are the owner of both and have read/write access to both.