Storage Access Framework – New and Improved File Browsing in Android 4.4

Written by: on November 13, 2013

With the release of Android 4.4 KitKat, users now have a smoother way of accessing and creating files over multiple locations.

Users will no longer be presented with a plain old boring provider chooser dialog to select which app is going to handle the requested action. Instead, with the new Storage Access Framework, users will be taken to new UI, in which all the content for the specific action can be seen.This new UI makes it easier for the user to navigate and find files they wish to share or even create across the multiple content providers.

What does this mean for developers?  Apps that are requesting content will need to be updated to support the new Storage Access Framework, while apps that wish to share their content to other apps will also need to be updated to allow access by the new framework.  This article will cover apps requesting content.

Requesting Content from a Provider

The below code is an example of how to request a Bitmap from the new Storage Access Framework.

For developers, the ACTION_OPEN_DOCUMENT tells the OS that the app wants to connect to all available document providers that allow files to read from, while the CATEGORY_OPENABLE filters the results to show only files that can be opened. The results then are further filtered through the supplied intent type, which acts as the MIME_TYPE.  After the code above is executed, the user will be taken to screen similar to the screenshot below.

Image from the Storage Access Framework Article on the Android Developers Website

As you can see from the screenshot, the user can easily switch between content providers and is always able to change content providers as needed if they aren’t able to find the desired file in one of the providers.  Once a provider is selected, the filtered results for that provider will be displayed.  The provider list will disappear, and the faded area will be brought to the front of the screen.  The screen should then look similar to the screenshot below.

Image from the Storage Access Framework Article on the Android Developers Website

After the desired file is selected, the OS will then return the user to the App that originally requested the content.  As a developer, the onActivityResults(requestCode, resultCode, data) method from within the Activity that requested the content will be called.  As with all results, the developer should verify that everything was A-Okay and that the request code matches what was sent in the first block of code.  The Intent data will contain a Uri which links to the document the user selected.   An example of what this code might look like can be seen below.

As this example (and probably all use cases) involved fetching the data from the file, an AsyncTask is used to handle parsing the Bitmap from the selected file (for the case of this example).  This processing should not be done on UI Thread and has not been supported to be handled on the UI Thread in a very long time. An example of Uri processing can be seen below.

Once processed, the Bitmap is then sent back to the UI Thread, at which point the app can display it to the user within app.

It is also possible to retrieve an InputStream from the Uri of the user selected file.  Below is example code for reading in a text file from the InputStream.

Sending Content to a Provider

The new Storage Access Framework also includes the ability to write data to a file of a ContentProvider.  The first section of code only needs a small amount of modification to handle this situation.

The action was switched to ACTION_CREATE_DOCUMENT, informing the content providers of the wish to create a new document with the set MIME_TYPE.  Though changeable later, the App can also send a name for the new document.

The same document selector from before is shown, and the user then selects where the file should be saved.  At this point, the code calls onActivityResult(requestCode, resultCode, data) again with the Uri to the newly created file.  The developer should write the data to the file and close the stream.

This piece is the third of eight in our KitKat Developer’s Guide. Check back later this week for new updates or follow us on twitter.

Matthew McConnell

Matthew McConnell

Matthew McConnell is a Software Engineer at Double Encore, specializing in Android Development.

Add your voice to the discussion: