In a world where phones are still sold with only 16GB (much less the odd 8GB devices) of storage space, managing free space can be a challenge. As a developer, this can cause issues when trying to store larger files on a user’s device that are crucial to the experience of using your apps. Luckily Android 7.1 adds a new feature to make everyone’s life a little easier: storage manager intents!

Now, whenever your application is going to save data on the device make sure to check if there’s space (you should be doing that anyways, using the StatFs class*) and if there’s not, fire an intent with the StorageManager.ACTION_MANAGE_STORAGE action and give users an option to clean up their unused apps, old photos (which should be getting backed up with Google Photos if they know what’s good for them), and stale downloads.

So for users on Android 7.1+ devices, something similar to the following would allow them to choose which old data they want to get rid of to clean up their device:

if(fileSize > availableFreeSpace) { if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) { //Clean up your old stuff! startActivityForResult(new Intent(StorageManager.ACTION_MANAGE_STORAGE, REQUEST_CODE)); } else { //Do things for older devices. } }

This drops them into the UI for cleaning up, which is built into the system:

blog-post-image-2_storage-manager-intents_er-copy

When your Activity returns with a result, you’ll be able to tell if they’ve cleared out any space, recheck free space, and save your precious data. The world continues to turn and your users get a working app. If there’s still not enough space, you’ll have to send the user out of the app, but that’s the same experience that would happen pre android 7.1.

It’s important to remember that not all Android users are on high-end devices, and some low to mid-tier hardware may have less storage. When saving large files is a key part of your app, storage is always going to be an issue. Using the new Storage Manager intent in Android 7.1, you can make it as painless as possible for users to continue using your apps regardless of the state of their storage.

For an example of using StatFs to determine free disk space, please see: https://goo.gl/J9uYEz