Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

Quick Tip: Enabling the Android Move To SD Card Feature

Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

This Android Quick Tip will show you how to allow your end-users to optionally save valuable device storage space by choosing to store your application on their removable SD card.

Final Result Preview

This tutorial will take an existing Android application and add the ability for the user to move the application to their SD card. This feature was introduced in Android 2.2, but does not require the application to use the Android 2.2 SDK as the minimum supported SDK version. Even your Android 1.5 application can use this feature.

Step 1: Preparation

We begin this tutorial by obtaining the version of the phrasebook Android application used in a previous tutorial called Android SDK: Building a Localized Phrasebook. Download the source code if you want to perform these steps yourself, or just follow along and use these steps with your own Android applications.

If the user has Android 2.2 installed on their device and you haven’t applied this tip, they’ll see a tantalizingly useful, yet grayed out, button when they try to move the application to their SD card. Developers must specifically enable this feature within their specific applications.

Move to SD Card In App

Step 2: Modify the Manifest

Edit the AndroidManifest.xml file of the application to add an entry for “android:installLocation” to the <manifest> tag:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mamlambo.article.phrasebook" android:versionCode="2" android:versionName="1.0.1" 
android:installLocation="auto">
...
</manifest>

The value is set to “auto” so that the user can decide where to install the application. If this value is not present at all, the default behavior is to only allow installation to internal storage and not to give the user a choice. The other possible values to use are “internalOnly” which is the same as the behavior without this setting, and “preferExternal,” which will install the application on the SD card first, if it’s available and there is room.

Keep the “minSdkVersion” the same as before; it need not be 8 to match Android 2.2:

<uses-sdk android:minSdkVersion="7" />

Step 3: Update the Build Target

The build target, however, needs to be updated to at least API Level 8 (Android 2.2), otherwise you’ll get the following error:


error: No resource identifier found for attribute 'installLocation' in package 'android’

Change the build target by editing the project properties (right-click on the project in Eclipse), and choose a target with at least API Level 8:

Move to SD Card 2.2

Step 4: Load it Up

Now, load the application on a 2.2 device (or emulator). Go to the application management area and you’ll now see that it can be moved to the SD Card!

Move to SD Card available

When to Use This Feature

In the future, most Android applications should enable the ability for users to choose the application install location. However, for some applications, this feature doesn’t make as much sense. For example, if you have an app widget, live wallpaper, or other sort of service or utility application (such as a file manager), your application won’t be of any use if the SD card is removed. This is supported, though. For instance, a live wallpaper on the SD card will simply revert to the default wallpaper and the user can reselect your live wallpaper when the SD card is reinserted.

Although users may not remove their SD cards frequently, the SD card is unmounted when it’s mounted via USB for use on a computer. This will potentially increase the frequency that the user will have to reconfigure apps that are affected. For a full list of apps that are affected when removed, see this Google Android reference.

Conclusion

Now your users can have the choice on whether or not to put your application on their SD card. Allowing this feature is important as users download more and more applications and fill up their Android devices with content. If you provide this option now, you’ll have a distinct advantage over other applications. It only takes a few minutes, yet enables a very powerful feature. I know I look for it; I’m constantly getting warnings about being low on space on my good old Nexus One.

Please, update your applications to allow this and resubmit them to the market. I’d appreciate it and so will the rest of your users!

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Advertisement