[英]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 时看到了三个解决方案
FileDescriptor
from a contentResolver and read the file from the contentResolver, writing it to your App's private storage area.FileDescriptor
并从 contentResolver 中读取文件,将其写入您的 App 的私有存储区域。 You can then get a Java File Object as normal from this copy of the file.FileDescriptor
from a contentResolver and read it in to a in memory Byte array.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.File
object 创建请求正文或多部分部分。FileDescriptor
object instead of a File
ObjectFileDescriptor
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.