0

A FileProvider can only generate a content URI for files in directories that you specify beforehand.

Our app allows users to change our default save directory to an arbitrary folder on their device's SD Card.

We have no way of foreseeing such paths and specifying them in the provide_paths.xml file.

Has anybody had this problem before and have you managed to modify these paths in runtime?

Gubatron
  • 6,222
  • 5
  • 35
  • 37
  • How are you allowing users to select a save directory? – ianhanniballake Mar 31 '17 at 05:56
  • with the Android Storage Access Framework, quite the hack when you were doing all your file I/O by wrapping a C/C++ library prior to the SAF BS got introduced and handicapped access to the SD card. Android just keeps getting harder and harder to use for us BitTorrent developers. – Gubatron Apr 02 '17 at 02:59
  • Thankfully, any C/C++ library can be built to accept [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) instead of file paths, which are fully supported by SAF (via a [technique like this](http://stackoverflow.com/a/24747137/1676363) and [`openAssetFileDescriptor`](https://developer.android.com/reference/android/content/ContentResolver.html#openAssetFileDescriptor(android.net.Uri,%20java.lang.String)) on the document Uri – ianhanniballake Apr 02 '17 at 03:39

2 Answers2

2

If you are using the Storage Access Framework, there's no reason to use FileProvider at all. You can pass the document URIs you have to other apps (making sure you also include FLAG_GRANT_READ_URI_PERMISSION) just the same as you could pass them a Uri generated by FileProvider.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
0

FileProvider cannot serve files from SD card or other removable media.

You cannot specify such folders in the xml.

If the user chooses a folder on external memory then you do not need to change paths in xml. If you call getUriForFile with the right File object then you are done.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • this seems to have worked https://github.com/frostwire/frostwire/commit/8e01cf583fe1f36d8e5a5d7224eefa550a978ed8 but I'm only testing now on an AVD SD storage, gotta test on a real device. – Gubatron Mar 31 '17 at 14:50
  • On a real device you sure cannot serve files from SD card. – greenapps Mar 31 '17 at 14:55
  • I am unclear on this answer as FileProvider does make provision for declaring external paths: Represents files in the root of the external storage area. https://developer.android.com/reference/android/support/v4/content/FileProvider.html – LordParsley Jun 21 '17 at 10:30