如何使用 Content Provider 在两个应用程序之间传递二进制数据?
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将数据作为 Base64 字符串提供?
Why not giving the data as Base64 string?
您尝试过使用流吗?这将起作用
从文档中:
“如果表条目是 content: URI,则永远不应该尝试直接打开并读取文件(一方面,权限问题可能导致此操作失败)。相反,您应该调用 ContentResolver.openInputStream() 来获取 InputStream 对象您可以用它来读取数据。”
链接: http://developer.android.com/guide/topics/providers /content-providers.html
如果您使用意图,也许是这样的:
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: