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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds