写一个私人的访问文件复制到另一个应用程序的文件目录 [英] Write a private access file to another app's files directory

查看:148
本文介绍了写一个私人的访问文件复制到另一个应用程序的文件目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个应用程序具有相同的sharedUserId。当我在APP1使用code

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.

我想写从APP1一个文件到APP2的存储。 (我做,当然需要确保APP2的文件目录中存在第一)

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

推荐答案

首先,切勿使用完整路径的内部存储类似 /数据/数据​​。让操作系统给你的路径(例如,通过 Context.getFilesDir() Environment.getExternalStorageState() )。不要在那里的数据是做出假设。

First of all, NEVER use a full path to internal storage like /data/data. Let the operating system give you the path (for example, via Context.getFilesDir() or Environment.getExternalStorageState()). Don't make assumption on where the data is.

其次 - 你已经在做了!不同于文件 Context.openFileOutput 已prepends /数据/数据​​/ [包] 来你的路径,所以你不需要指定。只要指定文件名。

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.

如果你真的认为它是安全的和必要的,如果两个应用程序采用了android共享相同的用户ID:在清单sharedUserId,您可以通过使用 Context.createPackageContent得到其他应用程序的背景下, ()和使用CONTEXT_RESTRICTED,然后用openFileOutput只有文件名。

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.createPackageContent() and use CONTEXT_RESTRICTED, then use openFileOutput with only the file name.

这篇关于写一个私人的访问文件复制到另一个应用程序的文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆