范围存储 - 如何让一个应用程序将文本文件共享给另一个应用程序

发布于 2025-01-09 14:39:04 字数 973 浏览 0 评论 0原文

我们有两个相互共享数据的应用程序。应用程序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2025-01-16 14:39:04

将文本文件保存在公共目录中,例如下载、文档

Intent shareIntent = new Intent();
String filename = ".bisym";
String fullPath = Environment.getExternalStorageDirectory() +Environment.DIRECTORY_DOWNLOADS + "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);

添加清单

<queries>
        <package android:name="com.example.yoursecondapp" />
         
</queries>

save text file in public directory Like Download , Document

Intent shareIntent = new Intent();
String filename = ".bisym";
String fullPath = Environment.getExternalStorageDirectory() +Environment.DIRECTORY_DOWNLOADS + "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);

Add Manifest

<queries>
        <package android:name="com.example.yoursecondapp" />
         
</queries>
山川志 2025-01-16 14:39:04

推荐的方法是使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文