简体   繁体   English

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

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

I have the uri of my files.我有我的文件的 uri。 Now I need to get its real path to send them.现在我需要得到它的真实路径来发送它们。 (the uri is like: "content://some-path"). (uri 就像:“content://some-path”)。

This is for Android 9这适用于 Android 9

So with Android 9+ you can only use file paths inside you applications private storage area otherwise you have to use URI's and ContentResolver s因此,对于 Android 9+,您只能在应用程序私有存储区域内使用文件路径,否则您必须使用 URI 和ContentResolver

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

So I see three solutions when using Retrofit2所以我在使用 Retrofit2 时看到了三个解决方案

  1. Get a Java FileDescriptor from a contentResolver and read the file from the contentResolver, writing it to your App's private storage area.从 contentResolver 中获取 Java FileDescriptor并从 contentResolver 中读取文件,将其写入您的 App 的私有存储区域。 You can then get a Java File Object as normal from this copy of the file.然后,您可以正常从此文件副本中获取 Java 文件 Object。 This is similar to stackoverflow.com/a/52814247/3518278 as suggested by ViVekH这类似于 ViVekH 建议的stackoverflow.com/a/52814247/3518278
  2. Get a Java FileDescriptor from a contentResolver and read it in to a in memory Byte array.从 contentResolver 中获取 Java FileDescriptor并将其读入 memory 字节数组中。 I'm not a Retrofit2 user but I believe you can create Request Body or Multipart part from a Byte array instead of a Java File object.我不是 Retrofit2 用户,但我相信您可以从字节数组而不是 Java File object 创建请求正文或多部分部分。
  3. Raise a feature request with RetroFit2 / OKHTTP to be able to give it a Java FileDescriptor object instead of a File Object使用 RetroFit2 / OKHTTP 提出功能请求,以便能够为其提供 Java FileDescriptor object 而不是File Object

Note with the contentResolver you can query it to get the "Display Name" as the filename part of the Path.请注意contentResolver您可以查询它以获取“显示名称”作为路径的文件名部分。

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

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