I am looking for a way to transfer files between applications running in the following scenario:

  • We have a file list in the application, a person clicks the Share button and we show him a dialog to select applications that can receive the file. For example, he can choose Gmail to create a letter to which the selected file will be attached or he can send this file via Bluetooth.
  • The file may be in the private directory of our application.
  • A file can be located anywhere in the file system, except for places requiring root privileges.
  • The application receiving the file may not have READ_EXTERNAL_STORAGE permissions.
  • It should work from API level 14 to the newest versions.

As far as I understand, the only way suitable for all conditions is the implementation in my FileProvider application or your own ContentProvider , which will issue the content URI for the file, and we will attach this URI to ACTION_SHARE Intent with setting FLAG_GRANT_READ_URI_PERMISSION . Is there a simpler alternative?

    2 answers 2

    Try FileProvider.getUriForFile () to get the file URI, and Intent.FLAG_GRANT_READ_URI_PERMISSION to pass the URI along with read permissions.

    Read more here and here .

    I can’t figure out if it will work at all with any file that the source application has access to. If, after all, not to everyone, then it is possible to copy files for which it is impossible to transfer read permission to a more accessible place.

    • Thank you for your answer, I know the FileProvider option, but FileProvider has serious limitations that do not fit the third point in my question, and I don’t want to copy files to a more accessible place. Probably I implement my ContentProvider, it fits all items, and it will work with any file that the source application has access to, but ContentProvider requires a lot of boring code and I wanted to know if there is a simpler alternative. - Dima

    As a result, I found the undocumented feature of FileProvider , which allows sharing files from anywhere on the device, this is the root-path element:

     <paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="my_root"/> </paths> 

    When using the root-path , FileProvider fully satisfies all the features I have requested. The implementation is only a dozen lines, FileProvider an excellent component.