1

I'm looking for a way to share files between apps running in the following scenario:

  • In our app, there is a list of files. The user clicks the Share button, and we show him the selection dialog that displays list of apps that can take the file. For example, user can choose Gmail, to create the letter and attach to this letter selected file or user can transfer the file via Bluetooth.
  • The file can be in a private directory of the our app.
  • The file can be anywhere in the file system excepting for the dirs that requires root access.
  • App that receives the file may not have READ_EXTERNAL_STORAGE permission.
  • It should work from API level 14 and up to the newest versions.

As I understand, the only way to fulfill all conditions is the implementing in my app custom ContentProvider or FileProvider. That ContentProvider will give the content URI for the file, and we attach that URI to the ACTION_SHARE Intent with the FLAG_GRANT_READ_URI_PERMISSION.

I worried that I have missed some other simple and clean solution. Whether is some other reliable alternative to ContentProvider exists at all, and if it exists, than what is it?

Dmitry Ratty
  • 425
  • 1
  • 10
  • 13
  • 1
    Using basic `FileProvider` for your scenario requires two lines of Java code, a four-line XML file, and less than a dozen lines in the manifest. How much simpler are you expecting to get? – CommonsWare Jan 13 '17 at 20:06
  • @CommonsWare thanks for your previous response. I haven't looked close at FileProvider requirements, but I have seen some ContentProviders, and to my taste they requiring a lot of boilerplate code. However, I'm fine to use it, just have some perfectionist curiosity about other ways. Sorry if I'm bothering. – Dmitry Ratty Jan 13 '17 at 20:16
  • 1
    Implementing a `ContentProvider` is aggravating. `FileProvider` is already implemented. You either use it directly, or you subclass it for minor compatibility fixes (e.g., the one that I use has one three-line method, and that's it). Admittedly, `FileProvider` will not necessarily cover everything in your scenario. Notably, you have no means of using `FileProvider` with removable storage, since you do not know the paths at compile time. – CommonsWare Jan 13 '17 at 20:19
  • @CommonsWare yep, removable storage path in FileProvider is the issue that I will look next. I thought there is some way to include in FileProvider just everything that starts from "/", but if there is not, then I will implement custom ContentProvider, or try override FileProvider behavior. Thanks again and if you add your comments as responses, I'll accept it in both questions. – Dmitry Ratty Jan 13 '17 at 20:34
  • 1
    "I thought there is some way to include in FileProvider just everything that starts from "/"" -- no, you are limited to roots in your app's portion of internal storage, plus external storage. "I will implement custom ContentProvider, or try override FileProvider behavior" -- I have pondered adding removable storage support to [my `StreamProvider`](https://github.com/commonsguy/cwac-provider) (an enhanced `FileProvider`), but I don't have it yet, sorry. And my comments don't really answer the question as written, since you are looking for options *other* than `FileProvider`. – CommonsWare Jan 13 '17 at 20:42
  • @CommonsWare my English is weak and likely I wasn't enough clear. My question are about whether is some other way exists at all, and only if it exists, than what is it. I just was worried that I have missed some other simple and clean solution. And as I understand, no other common ways exists. So answer is to use ContentProvider or FileProvider. – Dmitry Ratty Jan 13 '17 at 20:56

1 Answers1

0

As a result, I found an undocumented feature in FileProvider, which allows you to share files from anywhere on the device. It's root-path element:

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

When using root-path,FileProvider perfectly meets all my requirements. The implementation requires only a dozen of lines, FileProvider is an excellent component.

Dmitry Ratty
  • 425
  • 1
  • 10
  • 13