-3

I have the uri of my files. Now I need to get its real path to send them. (the uri is like: "content://some-path").

This is for Android 9

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
maryrio7
  • 310
  • 3
  • 18

1 Answers1

1

So with Android 9+ you can only use file paths inside you applications private storage area otherwise you have to use URI's and ContentResolvers

See https://developer.android.com/training/data-storage/files/external-scoped for details of the changes in 9+

So I see three solutions when using 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. You can then get a Java File Object as normal from this copy of the file. This is similar to stackoverflow.com/a/52814247/3518278 as suggested by ViVekH
  2. Get a Java FileDescriptor from a contentResolver and read it in to a in memory Byte array. 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.
  3. Raise a feature request with RetroFit2 / OKHTTP to be able to give it a Java FileDescriptor object instead of a File Object

Note with the contentResolver you can query it to get the "Display Name" as the filename part of the Path.

Andrew
  • 8,198
  • 2
  • 15
  • 35