范围存储 - 如何让一个应用程序将文本文件共享给另一个应用程序
我们有两个相互共享数据的应用程序。应用程序 A 创建一个文本文件,其中包含要由应用程序 B 读取的参数。然后应用程序 B 读取应用程序 A 创建的文本文件,其中包含要用于某些功能的参数。
我想知道,有什么方法可以使用范围存储来实现这一点吗?我找不到任何解决方案。我不确定这是否是 MANAGE_EXTERNAL_STORAGE
的有效用例。
编辑:这是来自应用程序 A 的 FileProvider 代码:
Intent shareIntent = new Intent();
String filename = ".bisym";
String fullPath = Environment.getExternalStorageDirectory() + "bisym/" + filename;
File file = new File(fullPath);
Uri uriToText = FileProvider.getUriForFile(mContext, "com.octal.provider2", file);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToText);
shareIntent.setType("text/plain");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(shareIntent);
我如何从应用程序 B 解析此文件?在 App BI 中,还想将此文件复制到其自己的私有应用程序目录中。
We have two apps that share data between each other. App A creates a text file with parameters to be read by App B. Then App B reads the text file created by App A with the parameters to be used for certain functionality.
I was wondering, is there any way this can be accomplished using scoped storage? I couldn't find any solution. I am not sure if this is a valid use case for MANAGE_EXTERNAL_STORAGE
.
EDIT: Here is my FileProvider code from App A:
Intent shareIntent = new Intent();
String filename = ".bisym";
String fullPath = Environment.getExternalStorageDirectory() + "bisym/" + filename;
File file = new File(fullPath);
Uri uriToText = FileProvider.getUriForFile(mContext, "com.octal.provider2", file);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToText);
shareIntent.setType("text/plain");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(shareIntent);
How would I parse this file from App B? In App B I would also like to copy this file into its own private app directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将文本文件保存在公共目录中,例如下载、文档
添加清单
save text file in public directory Like Download , Document
Add Manifest
推荐的方法是使用 FileProvider,但您需要共享目录才能为文件生成临时内容 URI。
这里详细解释了您需要遵循的步骤:https://developer.android。 com/training/安全文件共享
The recommended way is using FileProvider, but you need to share your directory in order to generate a temporary content URI for the file.
The steps you need to follow are explained in detail here : https://developer.android.com/training/secure-file-sharing