从内容提供者返回内存映射的InputStream?

发布于 2024-08-07 17:05:04 字数 549 浏览 4 评论 0原文

在内容提供者消费者的客户端,我可以做这样的事情,以获得适当的输入流来读取图片:

InputStream is = getContentResolver().openInputStream(pictureUri);

这是一个很好的API,并且将在服务器端,实际的内容提供者导致调用:

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  // Open a proper ParcelFileDescriptor, most likely using openFileHelper(uri, mode)
}

但是如果映射到 URI 的图片不在文件系统上找到,而是作为内存资源或动态生成,该怎么办?

我可以创建一个内存映射的 FileInputStream 或其他任何东西,这样我就不需要将临时文件保存到磁盘,而只需将其返回到我的内容提供商消费者?

The the client side of a content provider consumer I can do something like this, to get a proper InputStream for reading the picture:

InputStream is = getContentResolver().openInputStream(pictureUri);

It is a nice API, and will on the server side, the actual content provider result in a call to:

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  // Open a proper ParcelFileDescriptor, most likely using openFileHelper(uri, mode)
}

But what if the picture mapped to the URI is not to be found on the filesystem, but as a memory resource, or generated on the fly.

Can I create a memory mapped File or InputStream, or anything else, so that I am not required to save a temporary file to disk, just to be able to return it to my content provider consumer?

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

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

发布评论

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

评论(2

终弃我 2024-08-14 17:05:05

从 android-9 开始,您可以使用 ParcelFileDescriptor 创建管道。你可以将最多 64k 的数据塞进去并完成,或者你可以设置另一个线程来在读取后填充它。有关更多详细信息,请参阅此处的答案:

自定义 ContentProvider - openInputStream()、openOutputStream()

As of android-9, you can make a pipe using ParcelFileDescriptor. You can stuff up to 64k in it and be done with it, or you can set up another thread to fill it after its been read. See the answer here for more detail:

Custom ContentProvider - openInputStream(), openOutputStream()

弃爱 2024-08-14 17:05:04

这很难。您可能可以使用任何可以使用 Socket 接口的东西。我还没有这样做,但这就是让我这么想的原因:

http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#fromSocket(java.net.Socket)

理论上,Socket 可以是一个 Internet资源,或者大多数东西......如果你愿意在套接字级别工作。我可能会放弃并创建临时文件。也许这让我成为一个胆小鬼。

This is tough. You might be able to get away with using anything that can use a Socket interface. I haven't done it, but this is what makes me think so:

http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#fromSocket(java.net.Socket)

And a Socket could, in theory, be an Internet resource, or most anything...if you're willing to work at the Socket level. I would probably just give up and create the temporary file. Perhaps that makes me a coward.

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