如何使用 Content Provider 在两个应用程序之间传递二进制数据?

发布于 2024-10-10 10:07:52 字数 449 浏览 0 评论 0原文

我需要使用 Content Provider 在两个 Android 应用程序之间传递一些二进制数据(sharedUserId 不是一个选项)。

我不想将数据(存储为文件的保存游戏,大小小于20k)作为文件传递(即覆盖openFile()),因为这将需要一些复杂的临时文件方案来应对多个并发内容提供商访问和正在运行的游戏。

我想在互斥锁下将文件读入内存,然后以最简单的方式传递二进制数组。 我该怎么做?

由于 openFile() 的返回类型,似乎不可能在内存中创建文件。

query() 需要返回一个 Cursor。使用 MatrixCursor 是不可能的,因为它在读取时将 toString() 应用于所有存储的对象。

我需要做什么?实现自定义光标?该类有 30 个抽象方法。 我是否读取该文件,将其放入 SQLite 数据库并返回游标?

这个看似简单的任务的复杂性令人难以置信。

I need to pass some binary data between two android apps using Content Provider (sharedUserId is not an option).

I would prefer not to pass the data (a savegame stored as a file, small in size < 20k) as a file (ie. overriding openFile()) since this would necessitate some complicated temp-file scheme to cope with concurrency with several content provider accesses and a running game.

I would like to read the file into memory under a mutex lock and then pass the binary array in the simplest way possible.
How do I do this?

It seems creating a file in memory is not a possibility due to the return type of openFile().

query() needs to return a Cursor. Using MatrixCursor is not possible since it applies toString() to all stored objects when reading it.

What do I need to do? Implement a custom Cursor? This class has 30 abstract methods.
Do I read the file, put it in a SQLite db and return the cursor?

The complexity of this seemingly simple task is mindboggling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

岛徒 2024-10-17 10:07:52

为什么不将数据作为 Base64 字符串提供?

Why not giving the data as Base64 string?

虫児飞 2024-10-17 10:07:52

您尝试过使用流吗?这将起作用

从文档中:
“如果表条目是 content: URI,则永远不应该尝试直接打开并读取文件(一方面,权限问题可能导致此操作失败)。相反,您应该调用 ContentResolver.openInputStream() 来获取 InputStream 对象您可以用它来读取数据。”

链接: http://developer.android.com/guide/topics/providers /content-providers.html

如果您使用意图,也许是这样的:

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  InputStream is = getContentResolver().openInputStream(intent.getData())
...

Have you tried using streams? That will work

From the documentation:
"If the table entry is a content: URI, you should never try to open and read the file directly (for one thing, permissions problems can make this fail). Instead, you should call ContentResolver.openInputStream() to get an InputStream object that you can use to read the data."

Link: http://developer.android.com/guide/topics/providers/content-providers.html

Maybe something like this, if you are using Intents:

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  InputStream is = getContentResolver().openInputStream(intent.getData())
...

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