Open files using the Storage Access Framework (original) (raw)

Android 4.4 (API level 19) introduces the Storage Access Framework (SAF). The SAF lets users browse and open documents, images, and other files across all of their preferred document storage providers. A standard, easy-to-use UI lets users browse files and access recent files in a consistent way across apps and providers.

Cloud or local storage services can participate in this ecosystem by implementing a[DocumentsProvider](/reference/android/provider/DocumentsProvider) that encapsulates their services. Client apps that need access to a provider's documents can integrate with the SAF with a few lines of code.

The SAF includes the following:

SAF offers the following features:

Overview

The SAF centers around a content provider that is a subclass of the [DocumentsProvider](/reference/android/provider/DocumentsProvider) class. Within a document provider, data is structured as a traditional file hierarchy:

Figure 1. Document provider data model. A root points to a single document, which then starts the fan-out of the tree.

Note the following:

Control flow

The document provider data model is based on a traditional file hierarchy. However, you can physically store your data however you like, as long as you can access it using the [DocumentsProvider](/reference/android/provider/DocumentsProvider)API. For example, you can use tag-based cloud storage for your data.

Figure 2 shows how a photo app might use the SAF to access stored data:

Figure 2. Storage Access Framework flow.

Note the following:

In Figure 3, the user is selecting the Downloads folder from a picker opened in a search for images. The picker also shows all of the roots available to the client app.

Folder selection in system picker.

Figure 3. Picker showing Downloads folder selected as a search location.

After the user selects the Downloads folder, the images are displayed. Figure 4 shows the result of this process. The user can now interact with the images in the ways that the provider and client app support.

Figure 4. Images stored in the Downloads folder, as viewed in the system picker.

Write a client app

On Android 4.3 and lower, if you want your app to retrieve a file from another app, it must invoke an intent such as [ACTION_PICK](/reference/android/content/Intent#ACTION%5FPICK)or [ACTION_GET_CONTENT](/reference/android/content/Intent#ACTION%5FGET%5FCONTENT). The user then selects a single app from which to pick a file. The selected app must provide a user interface for the user to browse and pick from the available files.

On Android 4.4 (API level 19) and higher, you have the additional option of using the[ACTION_OPEN_DOCUMENT](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT) intent, which displays a system-controlled picker UI that lets the user browse all files that other apps have made available. From this single UI, the user can pick a file from any of the supported apps.

On Android 5.0 (API level 21) and higher, you can also use the[ACTION_OPEN_DOCUMENT_TREE](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT%5FTREE)intent, which lets the user choose a directory for a client app to access.

Note: ACTION_OPEN_DOCUMENT isn't a replacement for ACTION_GET_CONTENT. The one you use depends on the needs of your app:

For more information about how to support browsing for files and directories using the system picker UI, see the guide aboutaccessing documents and other files.

Additional resources

For more information about document providers, take advantage of the following resources:

Samples

Videos