Android N’s Packs Storage Access Framework Allows Virtual Files While Locally Unavailable

0
139

Android N has one more hidden feature in the latest Android N Developer Preview 3. The latest and the greatest Android N build have Sustained performance mode and 48 other features on the changelog.

In addition to them, there’s one more feature and that is Storage Access Framework. It allows Android N to make files available virtually if those are not available there. SAF was available earlier also but Virtual files function was missing at that time.

Recently, Dropbox quoted that they are coming with similar functionality on their cloud services.

Virtual files are effectively placeholders for the real file. THese files does not contain any memory much more that the actual placeholder size. These fiels are retrieved upon the usage.

In previous versions of Android, your app could use the Storage Access Framework to allow users to select files from their cloud storage accounts, such as Google Drive. However, there was no way to represent files that did not have a direct bytecode representation; every file was required to provide an input stream.

Android N adds the concept of virtual files to the Storage Access Framework. The virtual files feature allows your DocumentsProvider to return document URIs that can be used with anACTION_VIEW intent even if they don’t have a direct bytecode representation. Android N also allows you to provide alternate formats for user files, virtual or otherwise.

To get a URI for a virtual document in your app, first you create an Intent to open the file picker UI. Since an app cannot directly open a virtual file by using the openInputStream()method, your app does not receive any virtual files if you include the CATEGORY_OPENABLEcategory.

After the user has made a selection, the system calls the onActivityResult() method. Your app can retrieve the URI of the virtual file and get an input stream, as demonstrated in the code snippet below.

(code removed since it may change)

Well, you can read more about the Android N features here.