3

I understand why a FileProvider is useful for sharing one app's private files (files in the app's Internal Storage) with another app, while controlling permissions.

The docs explain how it can also be used to share files in External Storage (SD Card, etc):

<external-path name="name" path="path" />

instead of

<files-path name="name" path="path" />

(https://developer.android.com/reference/android/support/v4/content/FileProvider.html)

What's the point of this? Why would you need to use a FileProvider for this? The other app already has access to the external storage, so all any other app would need is a file path, not a URI. What am I missing?

Mike Miller
  • 3,368
  • 5
  • 36
  • 49

2 Answers2

4

The other app already has access to the external storage

Not necessarily. If they do not hold READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions, they do not have access to external storage.

Also, it would not surprise me if, at some point in time, greater restrictions on external storage come into play (e.g., "work" and "home" sections through the Android Work initiative), where one app's view of external storage was different than another app's view of external storage, even while logically being the same user. Then, even if the "work" app had the right permissions, it might not be able to see a file put on external storage by a "home" app, unless the "home" app served them through a ContentProvider like FileProvider.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ok, this makes a lot of sense. But in any case I would still be able to browse the directory (if I knew where it was located) and find all the files myself, right? And that's not the case with Internal Storage files: "files saved to the internal storage are private to your application and other applications cannot access them (nor can the user)" – Mike Miller Oct 13 '14 at 21:03
  • @MikeMiller: "I would still be able to browse the directory (if I knew where it was located) and find all the files myself, right?" -- I do not know who "I" is in that sentence. If "I" is Mike Miller, a human, then yes, at least for today's devices, and presumably into the future. Though do bear in mind that Android 4.2+ tablets support multiple accounts, and one account user cannot view another account user's files, as they have separate locations for internal and external storage. – CommonsWare Oct 13 '14 at 21:26
  • @MikeMiller: And I apologize if you are not human. I for one welcome our new `$SPECIES` overlords. :-) – CommonsWare Oct 13 '14 at 21:27
2

One of the changes made in Android 4.4 relates to storage permission changes:

Your app can not read shared files on the external storage when running on Android 4.4, unless your app has the READ_EXTERNAL_STORAGE permission. That is, files within the directory returned by getExternalStoragePublicDirectory() are no longer accessible without the permission.

However, if you are providing files via a FileProvider, your app can have permission while the other app does not need to know where the file is located.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443