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; 
 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"); 
 OutputStream os = new FileOutputStream(f); 
 //Write data to the file


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


  • 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.

  • 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); }

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

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds