繁体   English   中英

从 URI 获取真实路径? Android 9+

[英]Get real path from URI? Android 9+

我有我的文件的 uri。 现在我需要得到它的真实路径来发送它们。 (uri 就像:“content://some-path”)。

这适用于 Android 9

因此,对于 Android 9+,您只能在应用程序私有存储区域内使用文件路径,否则您必须使用 URI 和ContentResolver

有关 9+ 中更改的详细信息,请参阅https://developer.android.com/training/data-storage/files/external-scoped

所以我在使用 Retrofit2 时看到了三个解决方案

  1. 从 contentResolver 中获取 Java FileDescriptor并从 contentResolver 中读取文件,将其写入您的 App 的私有存储区域。 然后,您可以正常从此文件副本中获取 Java 文件 Object。 这类似于 ViVekH 建议的stackoverflow.com/a/52814247/3518278
  2. 从 contentResolver 中获取 Java FileDescriptor并将其读入 memory 字节数组中。 我不是 Retrofit2 用户,但我相信您可以从字节数组而不是 Java File object 创建请求正文或多部分部分。
  3. 使用 RetroFit2 / OKHTTP 提出功能请求,以便能够为其提供 Java FileDescriptor object 而不是File Object

请注意contentResolver您可以查询它以获取“显示名称”作为路径的文件名部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM