Wednesday, February 4, 2015

Android beginner tutorial Part 76 External Storage

In this tutorial well cover External storage in Android.

Besides saving files internally, you can save them to an external storage that exists in every Android device. This can be a removable storage media like a memory card or an internal storage. Files saved this way can be read and modified by any other application and the user himself. It is stored as a file somewhere on the device (location specified by the developer) and can be transferred to a computer, etc.

The first thing you need to do when working with external storage is check whether the storage is actually accessible. This can be done using the getExternalStorageState() method of the Environment class:

if (Environment.MEDIA_MOUNTED.equals(state)) {
// Data is readable and writeable
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Data is read-only
} else {
// Cant read or write
}

There are 2 ways to access files on external storage - one is used on devices with Android API Level lower than 8, the other one on devices with API Level of 8 and greater. Almost nobody uses an Android device with API less than 8 nowadays, though.

With API levels 7 and lower, a function called getExternalStorageDirectory() is used to open a File object that represents the root of the external storage. When you get the File, you should write your data in this directory:

/Android/data/<packagename>/files/

Replace <packagename> with your full package name.

With API levels 8 and greater, use a function getExternalFilesDir() to open a File that represents the directory where your files should be saved. The method takes a type parameter that determines what subdirectory to save your files in. You can pass values like DIRECTORY_MUSIC, DIRECTORY_MOVIES, DIRECTORY_RINGTONES, DIRECTORY_PICTURES, etc. These are all static variables of the Environment class.

If the directory does not exist, it will be automatically created. When the application is uninstalled, all these directories and files will be removed too.

By putting your files in the appropriate directories, you ensure that they will be properly identified and categorized by the media scanner. For example, ringtones will be identified as ringtones and not music or podcast.

If you want to save files that are not meant to be exclusive to your application, but rather be shared with other apps and the user, and not removed after the application is uninstalled, then you should save them in public directories.

In API Level 8 or greater, use the getExternalStoragePublicDirectory() function, which also receives a type parameter, just like the getExternalFilesDir() function explained above.

In API Levels lower than 8, use getExternalStorageDirectory() to open the root folder, then navigate to one of the following directories: Music, Podcasts, Ringtones, Alarms, Notifications, Pictures, Movies or Download.

If you want to save cache files, use getExternalCacheDir() in Android API Levels 8 and greater. In older versions, use getExternalStorageDirectory() method to get to the root directory and then navigate to:

/Android/data/<packagename>/cache/

Thats all for today.

Thanks for reading!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.