This document discusses storage and content providers in Android. It notes that content providers were influenced by REST concepts from the web to allow sharing of data between apps via Uris. It outlines how content providers allow accessing data through streams or as database structures. The document also discusses how early Android emphasized files for sharing rather than content providers, but that Android is shifting more towards using content providers and Uris to identify shared content over direct file access.
2. PROVIDERS IN 60 SECONDS
OR THEREABOUTS. PLEASE DON'T TIME ME.
ContentProvider subclass
Serve streams or database-style structures
Principally for data exchange with other apps
Uri to identify content (or a collection)
ContentResolver as foundation for consuming that
content
3. ANDROID AT 10+
November 2007
Android announced
Android SDK beta released
Open Handset Alliance announced ¯_(ツ)_/¯
Development started well before 2007
4. THE WEB: SEEMINGLY POPULAR
COMPUSERVE AIN'T WALKIN' THROUGH THAT DOOR
Much of the Android philosophy was an attempt to apply
Web concepts to local environments, sans HTML
Example: startActivity() ~= hyperlink to other Web
page or site
Storage approach influenced by standard HTTP and REST
5. REST, SANS RELAXATION
GET, PUT, DELETE for reading, writing, deleting a resource
Various conventions for creating a resource (e.g., POST)
Dynamic access control (authentication headers, session
cookies, etc.)
Server determines where content comes from (static,
generated, etc.)
6. FILES
ORGANIZING BYTES SINCE 1950!
OS and language means for reading, writing, deleting, and
creating files
Static access control (permission bits, ACLs, etc.)
Static content
7. STREAMING PROVIDERS
A RESTED DEVELOPMENT
openInputStream(), openOutputStream(),
delete() for reading, writing, and deleting content by
Uri
Various conventions for creating content
(ACTION_CREATE_DOCUMENT)
Dynamic access control (permissions, temporary content
access)
Provider determines where content comes from (static,
generated, etc.)
8. THE VISION
NO, NOT PAUL BETTANY'S CHARACTER IN THE AVENGERS
Files are for things that you own, mostly internal storage
Providers are for things that you share including sharing
with the platform
MediaStore for things the user sees as files
ACTION_GET_CONTENT,
ACTION_OPEN_DOCUMENT, etc. for app-managed
content
9. WHAT WENT WRONG?
THROWING GOOGLE UNDER THE BUS AND RUNNING OVER
THEM REPEATEDLY
Limited documentation
Poor examples in AOSP and Google proprietary apps
No real leadership on this topic (until some recently)
Result: emphasis on files for sharing
10. THE TIMES, THEY ARE A-CHANGIN'
Android 4.4: No arbitrary filesystem access to removable
storage
Android 6.0: Runtime permissions for external storage
Android 7.0: FileUriExposedException
Android O: So limits on cached data
11. PLAYING NICE WITH ANDROID
CONSUMING CONTENT
Get a Uri
Proactively: ACTION_GET_CONTENT,
ACTION_OPEN_DOCUMENT, MediaStore, etc.
Reactively: content scheme in <data>, drag-and-
drop, clipboard, etc.
Use ContentResolver to work with it
Use OpenableColumns for display name and size
12. PLAYING NICE WITH ANDROID
BENEFITS OF ACTION_OPEN_DOCUMENT
Works with external storage, removable storage, Google
Drive, etc.
DocumentFile or DocumentFileCompat for
accessing metadata
takePersistableUriPermissions() for durable
access to the content
13. PLAYING NICE WITH ANDROID
SHARING CONTENT
FileProvider
StreamProvider from CWAC-Provider
Your own ContentProvider or
DocumentsProvider
Supply Uri to other apps, using
FLAG_GRANT_READ_URI_PERMISSION
14. BUT I NEED A FILE
Use a file picker library, not ACTION_GET_CONTENT
Create a Local Copy of the Content
Temporary (copy to cache, use, delete)
Not so temporary ("import")