Take Advantage of External Storage with Your Android App

Building apps for Android devices often includes the need to store large amounts of information on the device. As a developer, you have two choices for storing large amounts of data including the apps install folder or External Storage. Since many devices have limited internal storage for apps, it is recommended to use External Storage. Using External Storage is not a limitless storage medium as it is limited to the size for the SD (Secure Digital) Card installed.

To get started you need to add a uses permission into the AndroidManifest.xml file for your app. The permission you need to add is shown below:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

This XML snippet will need to be inserted before the closing </manifest> tag.

Another important caveat about External Storage is that it may or may not be available due to a number of reasons such as the user may not have an SD card installed or the system may be checking the SD cards for errors. Conveniently enough, Android does provide a method for detecting the current state of External Storage within the android.os package. The following method uses the Environment.getExternalStorageState() method to get a value, which is used to determine the state.

public static final boolean ExternalStorageWritable() 
 { 
 String state = Environment.getExternalStorageState(); 
 
 if (Environment.MEDIA_MOUNTED.equals(state)) 
 { 
 return true; 
 } 
 else 
 { 
 return false; 
 } 
 }

As you can see, by comparing the state value to the constant Environment.MEDIA_MOUNTED, we can easily create a method, which will provide us with the state. The state of External Storage does include values for read only, media checking, etc; however, for most applications you can ignore these states as the media is not yet writeable.

Now that we have permission to access External Storage as well as the tool to know if it is available we need to be able to access files. Again using the android.os package we can use the method Environment.getExternalStorageDirectory() to get the directory point to the root of the SD. Using this directory, we can now use classes within the standard java.io package to work with files. Listed below is a simple snippet of code used to create a new file in the download folder of the SD card.

File path = new File(Environment.getExternalStorageDirectory(),"download"); 
 File f = new File(path, "testfile.txt"); 
 f.createNewFile(); 
 OutputStream os = new FileOutputStream(f); 
 
 //Write data to the file
 
 os.close();

Conclusion

Using External Storage for your application is a great way to expand the amount of storage your app can use locally on the device. And from the code snippets above it is quite easy to detect the presence of external storage as well as utilize it. If you notice quite a few apps in the Android Market use the WRITE_EXTERNAL_STORAGE permission as well as requiring the phone to have external storage available. If your app is one that will require external storage then it would be a good idea to use the ExternalStorageWritable snippet above to detect the presence upon launching your app. In addition, it is a good idea to check for the presence of External Storage often as the user can remove it at any point. Nonetheless, adding support for External Storage within your application is quite easy to accomplish using the example above.



About the Author

Chris Bennett

Chris Bennett is a Manager with Crowe Horwath LLP in the Indianapolis office. He can be reached at chris.bennett@crowehorwath.com.

Related Articles

Comments

  • Android

    Posted by Arun Chandravanshi on 08/29/2012 08:02am

    this is very helpful to write file into external devices., But I feel problem writing images into external storage device.

    Reply
  • Cut down ExternalStorageWritable

    Posted by Peter on 08/04/2012 11:34am

    public static final boolean ExternalStorageWritable() { String state = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(state); }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds