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.

More by Author

Must Read