Android:从我的应用程序共享一个大文件(图像、文档等,...)而不在缓存 folder/temp 文件中创建副本

Android: Sharing a big file from my app (image, document, etc., ...) without creating a copy in a cache folder/temp file

我正在尝试将我的应用程序中的文件(任何类型的文件)与 phone 中的另一个应用程序共享(例如,通过“共享”菜单),或在 phone 的默认应用程序,但我需要在不在临时文件夹或缓存文件夹中创建文件副本的情况下执行此操作,然后使用该路径创建意图,就像通常所做的那样。

不同之处在于我已经在 phone 中拥有该文件,但它存储在某种自定义文件系统中(这是我们正在开发的应用程序,除其他外,它对文件进行了加密,这就是文件不在 phone 中的常规文件夹中的原因)。我可以轻松获取文件的字节数,创建 reader,等等;但到目前为止,我只找到了执行所需操作的常用方法:创建一个临时文件,将字节写入其中,然后使用该文件的路径创建一个意图,等等。

我是不是漏掉了什么?

有没有办法传递其他类型的抽象,而不是文件的路径,告诉其他应用程序或 Android,使用它从中获取文件(字节?) reader 或类似的东西?

I can easily get the file's bytes, create a reader, etc; but so far I have only found the usual way to do what I need: create a temporary file, write the bytes to it, then create an intent with the path to this file, and so on.

这已经好几年没用了,如果“使用此文件的路径创建一个意图”是指使用 Uri.fromFile().

Is there a way to pass, instead of the path to the file, other kind of abstraction that tells the other app or to Android, to use it to get the file (the bytes?) from a reader or something similar?

A Uri 不是文件。您通过 EXTRA_STREAMACTION_SEND Intent 上提供的 Uri 不必指向文件。事实上,不应该直接指向一个文件。

应该指向to a ContentProvider.

现在,ACTION_SEND 的常见 ContentProvider 实现是 FileProvider,这是一个 ContentProvider,它知道如何在文件系统上流式传输文件内容。但是,您不必使用它 — 您可以创建自己的 ContentProvider 从其他来源流式传输内容。

你可以 download for free an older edition of one of my books. It has a lot of material on ContentProvider, including ~50 pages in two chapters focused on how to implement custom providers. You would be using the streaming-style API more than the database-style API. Off the cuff, the Pipe example 可能是最接近你要做的事情。