Android Environment Class Interface detailed

Source: Internet
Author: User

Android application development, often use the environment class to get external storage directory, before accessing external storage must first determine whether the external storage is already available (mounted & available) state,
And you need to add external storage read and write permissions to the Androidmanifest.xml file.
Several static constants are provided in the environment class to identify the state of the external storage, which are string types
Media_bad_removal The storage media has been removed before it is mounted.
Media_checking is checking storage media.
media_mounted storage media is already mounted and the mount point is readable/writable.
Media_mounted_read_only storage media has been mounted, mount point is read only.
Media_nofs storage Media is a blank or unsupported file system.
media_removed storage media is removed.
media_shared storage media is being shared via USB.
media_unmountable storage media cannot be mounted.
media_unmounted storage media is not mounted.
The state of the external storage can be obtained through the static method Getexternalstoragestate (), if the program needs to read and write data in the external storage, it must first determine:

if (Environment.MEDIA_MOUNTED.equals (
Environment.getexternalstoragestate ())
|| ! Environment.isexternalstorageremovable ())

Then, add the externally stored read and Write permissions:

    <uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission Android : Name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

The path to the Android standard directory is also available in environment, provided as a string type.
The directory_alarms system reminds you of the standard directory where ringtones are stored.
DIRECTORY_DCIM camera's standard catalog for taking photos and videos.
Directory_downloads download the standard directory.
Directory_movies the standard catalogue of movie storage.
Directory_music Standard catalogue for music storage.
Directory_notifications system notifies the standard directory where the ringtone is stored.
Directory_pictures Standard catalogue for picture storage
Standard directory for directory_podcasts system broadcast storage.
Standard directory for directory_ringtones system ringtone storage.

Static File Getdatadirectory () obtains the directory of data (/data).
Static File Getdownloadcachedirectory () Gets the download cache directory. (/cache)
Static File getExternalStorageDirectory () obtains the external storage media directory. (/mnt/sdcard or/storage/sdcard0)
Static File Getrootdirectory () obtains the System home directory (/system)

In addition to using environment to get the storage directory, but also by the way to write the path, such as to read the external storage/mnt/sdcard directory files, you can directly use the full path in the program,
But this is not good, should be Android is too open, external storage directory of what is still for the firmware maker to know, but one thing is beyond doubt, is the Android framework layer inside
The return path for Environment.getdownloadcachedirectory () is already specified. Therefore, as far as possible in this way to obtain and store data, so as not to create different firmware manufacturers path differences.

Android's actual development also uses two very important cache directories, one is the application's own cache space, and the other is the external storage for the program to provide the cache space. What's the difference?
Children's shoes with LRUCache and Dislrucache should be known.
These two methods are obtained through context object contexts, and the files under both directories are emptied as long as the application is unloaded.
Context.getcachedir () Get the application's own cache directory
Context.getexternalcachedir () Gets the storage directory where the application is stored externally

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.