Android Environment interface details, androidenvironment

Source: Internet
Author: User

Android Environment interface details, androidenvironment

In Android Application Development, the Environment class is often used to obtain external storage directories. before accessing external storage, you must first determine whether the external storage is ready for use (mounted and available,
In addition, you must add the read and write permissions for external storage to the AndroidManifest. xml file.
The Environment class provides several static constants used to identify the States of external storage, all of which are of the String type.
MEDIA_BAD_REMOVAL: The storage media has been removed before being mounted.
MEDIA_CHECKING is checking the storage media.
The MEDIA_MOUNTED storage media has been mounted and the mount point can be read/written.
The MEDIA_MOUNTED_READ_ONLY storage media has been mounted and the mount point is read-only.
MEDIA_NOFS stores Blank Media or unsupported file systems.
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.
You can use the static getExternalStorageState () method to obtain the status of the External Store. To read and write data in the external store, you must first judge:

If (Environment. MEDIA_MOUNTED.equals (
Environment. getExternalStorageState ())
|! Environment. isExternalStorageRemovable ())

Then, add the read and write permissions for the External Store:

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

In Environment, the path of the Android standard directory is also provided as String.
Standard Directory for DIRECTORY_ALARMS system alert ringtones.
Standard Directory of photos and videos taken by the DIRECTORY_DCIM camera.
Standard Directory for DIRECTORY_DOWNLOADS download.
Standard Directory of DIRECTORY_MOVIES movies.
Standard Directory of DIRECTORY_MUSIC music.
Standard Directory for DIRECTORY_NOTIFICATIONS system notification ringtones.
Standard Directory for DIRECTORY_PICTURES Image Storage
Standard Directory for DIRECTORY_PODCASTS System broadcast.
Standard Directory for DIRECTORY_RINGTONES system ringtones.

Static File getDataDirectory () to obtain the data directory (/data ).
Static File getDownloadCacheDirectory () to obtain the download cache directory. (/Cache)
Static File getExternalStorageDirectory () to obtain the external storage media directory. (/Mnt/sdcard or/storage/sdcard0)
Static File getRootDirectory () Get the system home directory (/system)

In addition to using Environment to obtain the storage directory, you can also write the path to an end, for example, to read files in the external storage/mnt/sdcard directory, you can directly use the full path in the program,
However, this is not a good practice. Android is too open, and the directory of external storage should only be known by the firmware manufacturer. But there is no doubt that it is in the Android framework layer.
The returned path of Environment. getDownloadCacheDirectory () has been specified. Therefore, try to use this method to obtain and store data to avoid path differences caused by different firmware vendors.

In actual development of Android, two very important cache directories are used. One is the cache space of the application, and the other is the cache space provided by external storage for the program. What is the difference?
The shoes that have used LruCache and DisLruCache should be known.
The two methods are obtained through the Context object. As long as the application is uninstalled, the files in both directories will be cleared.
Context. getCacheDir () obtains the cache directory of the application.
Context. getExternalCacheDir () obtains the storage directory where the application is stored externally.


Android programming interface problems

The interface needs to be implemented. This interface has been implemented when you create an interface object.
Listener is the object instantiated by OnCheckedChangeListener. You can use:
Toggle. setOnCheckedChangeListener (new OnCheckedChangeListener ());
If there is no interface object, you will find that the interface needs to be implemented in the class!

The android development interface defines whether a class is a constant or not in the class, and explains the following code:

It is not a constant, and the constant declaration must use final. Static variables and static methods are declared here.

Related Article

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.