Android Environment class


Android Environment Class Tutorial and Examples.

In the android.os package is an Environment class that gives us acess to environment variables. In this tutorial we will explore several example snippets to understand several capabilities of this class.

Environment API Definition

The Environment class is a public class directly extending the java.lang.Object class:

public class Environment extends Object {}

Here's it's inheritance hierarchy:

java.lang.Object
   ↳    android.os.Environment

Public Directories

Here are public system defined dirextories:

No. Directory Description
1. DIRECTORY_ALARMS Standard directory in which to place any audio files that should be in the list of alarms that the user can select (not as regular music).
2. DIRECTORY_DCIM The traditional location for pictures and videos when mounting the device as a camera.
3. DIRECTORY_DOCUMENTS Standard directory in which to place documents that have been created by the user.
4. DIRECTORY_DOWNLOADS Standard directory in which to place files that have been downloaded by the user.
5. DIRECTORY_MOVIES Standard directory in which to place movies that are available to the user.
6. DIRECTORY_MUSIC Standard directory in which to place any audio files that should be in the regular list of music for the user.
7. DIRECTORY_NOTIFICATIONS Standard directory in which to place any audio files that should be in the list of notifications that the user can select (not as regular music).
8. DIRECTORY_PICTURES Standard directory in which to place pictures that are available to the user.
9. DIRECTORY_PODCASTS Standard directory in which to place any audio files that should be in the list of podcasts that the user can select (not as regular music).
10. DIRECTORY_RINGTONES Standard directory in which to place any audio files that should be in the list of ringtones that the user can select (not as regular music).

Public Environment Methods

Here are some public methods in the Environment class that you will want to use when working with this class:

No. Return Type Method Description
1. File getDataDirectory() Return the user data directory.
2. File getDownloadCacheDirectory() Return the download/cache content directory.
3. File getExternalStorageDirectory() Return the primary shared/external storage directory.
4. File getExternalStoragePublicDirectory(String type) Get a top-level shared/external storage directory for placing files of a particular type.
5. String getExternalStorageState() Returns the current state of the primary shared/external storage media.
6. String getExternalStorageState(File path) Returns the current state of the shared/external storage media at the given path.
7. File getRootDirectory() Return root of the "system" partition holding the core Android OS.
8. String getStorageState(File path) This method was deprecated in API level 21. use getExternalStorageState(File)
9. boolean isExternalStorageEmulated() Returns whether the primary shared/external storage media is emulated.
10. boolean isExternalStorageEmulated(File path) Returns whether the shared/external storage media at the given path is emulated.
11. boolean isExternalStorageRemovable() Returns whether the primary shared/external storage media is physically removable.
12. boolean isExternalStorageRemovable(File path) Returns whether the shared/external storage media at the given path is physically removable.

Quick Environment Class Examples.

Let's look at some quick examples:

1. How to Open a Directory

Let's say a path is being passed to us and we have to get and return a File object.

    private static File openDirectory(final String path) {
        File file = null;

        if (isExternalStorageAvailable()) {
            final File storageDirectory = Environment.getExternalStorageDirectory();

            file = new File(storageDirectory, path);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    file = null;
                }
            }
        }

        return file;
    }

2. How to determine if ExternalStorage is Available

We will return true if the external storage is available. If not we return false.

    public static boolean isExternalStorageAvailable() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

3. How to Open the Pictures Directory

We see how to open the system defined pictures directory. We will return the path of the directory holding application files on external storage.

    public static File openPublicPicturesDirectory() {
        if (isExternalStorageAvailable()) {
            return (AndroidUtils.isAtLeastFroyo()) ? Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES) : openDirectory("Pictures");
        }
        return null;
    }

4. How to Open the Music Directory

This method opens the the system defined music directory. We return the path of the directory holding application files on external storage. Let's say we have a separate utility class from which we can determine if the device is atleast Froyo.

    public static File openMusicDirectory(final Context context) {
        if (isExternalStorageAvailable()) {
            return (AndroidUtils.isAtLeastFroyo()) ? context.getExternalFilesDir(Environment.DIRECTORY_MUSIC)
                                                   : openDirectory("music");
        }

        return null;
    }

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post