Android 使用 ContentUri 在应用之间共享
Android sharing between apps using ContentUri
我想知道在 android 的相同设备上将信息从一个应用程序传递到另一个应用程序的最佳方法。
例如:
- 我打开 google 个应用程序并与我的应用程序 A 共享文档。
- Google 应用程序生成了一个意图并发送了一个内容 URI。从我的
理解,内容uri包含有关文件的信息
(文件名、文件大小、mimetype)和提取的能力
位于 google 应用缓存中的内容
设备。
- 当 App A 打开时,它会读取内容 URI。理想情况下,它
应该能够从内容 uri 中提取信息,并且
然后渲染图像。这意味着 App A 将显示共享的图像。在此示例中,google 应用程序共享一个文档,应用程序 A 想要在其自己的应用程序中打开并显示该文档。
令人困惑的部分
- 从网上查了一下,好像真的有人在尝试
从内容 URI 中提取文件路径。这需要你
有权访问另一个应用程序的缓存或存储 space
设备内。假设这是可能的。也让一些
假设可以提取文件路径。
看了一些文章后:
https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html
https://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html
https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html
看来,理想情况下,您永远不应该假设您可以提取文件路径,并且 google 已经进行了一些更新,这使得这不可能。
解决方法:
- 尽管我无法从
contentUri,我能够读取 contentUri 的字节
指向。所以我可以将它保存到一个与
App A 的本地缓存并传递该路径以获取渲染或传递
返回字节。这是指显示内容的 App A。那就是传递路径或字节,让我们假设它知道如何根据该信息显示它。
问题:
解决方法似乎并不理想,因为从技术上讲你是
将文件再次保存在设备上。有两个位置
相同的内容(google 应用存储和应用 A 的存储)。你也
必须管理何时删除您创建的应用程序 A 的文件。
这看起来并不理想,想知道什么是最好的
做法会是?或者这是预期的流量?
我也不知道
如果将字节传回而不是仅传递文件路径是理想的。
更新
更具体地说,我正在创建的应用程序是一个混合应用程序,我在其中使用 cordova 插件与网络应用程序进行交互。 Web 应用程序具有根据文件路径处理或显示共享文档的方法。所以理想情况下,我希望它与读取文件路径保持一致,这样网络应用程序支持的其他平台就不会中断。
任何建议表示赞赏,
D
Eventhough i'm not able to extract the file path from the contentUri, I'm able to read the bytes of what the contentUri is pointing to.
正确。这与您使用 HTTPS URL 的方式没有明显不同,在 HTTPS 中您也没有对内容的直接文件系统访问权限(在那种情况下,驻留在不同的服务器上)。
So I could save it to a file that is relevant to the local cache of App A and pass that path along to get render or pass the bytes back.
或者,只消耗字节。再次类比 HTTPS URL,无需将这些字节保存到磁盘即可使用它们。
The work around does not seem ideal because technically you are save the file again on the device. There are two locations with the same content ( google app storage and App A's storage). You also have to manage when to delete the App A's file that you created.
那就不要再在设备上保存文件了,直接用字节流就行了。同样,这与使用 HTTPS URL.
没有太大区别
This doesn't really seem ideal and was wondering what the best approach would be?
不要将字节写入磁盘。就用它们吧。
So ideally I want to keep it consistent with just reading the file path so that the other platforms that the web app supports does not break.
您的选择是:
改进 Web 应用程序代码,使本地文件路径成为 一个 可能的数据源,或
在复制该数据时遇到问题
毕竟,请记住,通过 ACTION_SEND
给你的 Uri
不一定是 content
Uri
。它很容易成为 http
或 https
Uri
。
我想知道在 android 的相同设备上将信息从一个应用程序传递到另一个应用程序的最佳方法。
例如:
- 我打开 google 个应用程序并与我的应用程序 A 共享文档。
- Google 应用程序生成了一个意图并发送了一个内容 URI。从我的 理解,内容uri包含有关文件的信息 (文件名、文件大小、mimetype)和提取的能力 位于 google 应用缓存中的内容 设备。
- 当 App A 打开时,它会读取内容 URI。理想情况下,它 应该能够从内容 uri 中提取信息,并且 然后渲染图像。这意味着 App A 将显示共享的图像。在此示例中,google 应用程序共享一个文档,应用程序 A 想要在其自己的应用程序中打开并显示该文档。
令人困惑的部分
- 从网上查了一下,好像真的有人在尝试 从内容 URI 中提取文件路径。这需要你 有权访问另一个应用程序的缓存或存储 space 设备内。假设这是可能的。也让一些 假设可以提取文件路径。
看了一些文章后:
https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html
https://commonsware.com/blog/2014/07/04/uri-not-necessarily-file.html
https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html
看来,理想情况下,您永远不应该假设您可以提取文件路径,并且 google 已经进行了一些更新,这使得这不可能。
解决方法:
- 尽管我无法从 contentUri,我能够读取 contentUri 的字节 指向。所以我可以将它保存到一个与 App A 的本地缓存并传递该路径以获取渲染或传递 返回字节。这是指显示内容的 App A。那就是传递路径或字节,让我们假设它知道如何根据该信息显示它。
问题:
解决方法似乎并不理想,因为从技术上讲你是 将文件再次保存在设备上。有两个位置 相同的内容(google 应用存储和应用 A 的存储)。你也 必须管理何时删除您创建的应用程序 A 的文件。 这看起来并不理想,想知道什么是最好的 做法会是?或者这是预期的流量?
我也不知道 如果将字节传回而不是仅传递文件路径是理想的。
更新
更具体地说,我正在创建的应用程序是一个混合应用程序,我在其中使用 cordova 插件与网络应用程序进行交互。 Web 应用程序具有根据文件路径处理或显示共享文档的方法。所以理想情况下,我希望它与读取文件路径保持一致,这样网络应用程序支持的其他平台就不会中断。
任何建议表示赞赏, D
Eventhough i'm not able to extract the file path from the contentUri, I'm able to read the bytes of what the contentUri is pointing to.
正确。这与您使用 HTTPS URL 的方式没有明显不同,在 HTTPS 中您也没有对内容的直接文件系统访问权限(在那种情况下,驻留在不同的服务器上)。
So I could save it to a file that is relevant to the local cache of App A and pass that path along to get render or pass the bytes back.
或者,只消耗字节。再次类比 HTTPS URL,无需将这些字节保存到磁盘即可使用它们。
The work around does not seem ideal because technically you are save the file again on the device. There are two locations with the same content ( google app storage and App A's storage). You also have to manage when to delete the App A's file that you created.
那就不要再在设备上保存文件了,直接用字节流就行了。同样,这与使用 HTTPS URL.
没有太大区别This doesn't really seem ideal and was wondering what the best approach would be?
不要将字节写入磁盘。就用它们吧。
So ideally I want to keep it consistent with just reading the file path so that the other platforms that the web app supports does not break.
您的选择是:
改进 Web 应用程序代码,使本地文件路径成为 一个 可能的数据源,或
在复制该数据时遇到问题
毕竟,请记住,通过 ACTION_SEND
给你的 Uri
不一定是 content
Uri
。它很容易成为 http
或 https
Uri
。