Android Storage access and directory
External storage for Android
Android supports external storage (case-insensitive filesystem with immutable POSIX permission classes and modes).
External storage can be provided through physical media, such as an SD card, or by encapsulating a portion of the internal storage, and the device can have multiple external storage instances.
permissions to access external storage
Starting with Android 1.0, write operations are protected by rights Write_external_storage .
Starting with Android 4.1, read operations are protected by rights Read_external_storage .
Starting with Android 4.4, apps can manage a specific package name directory on its external storage without obtaining write_external_storage permissions.
For example, an application with the package name Com.example.foo can freely access the android/data/com.example.foo/directory on the external memory.
External storage provides less protection for data, so the system should not store sensitive data on external storage.
In particular, configuration and log files should be stored in internal storage so that they can be effectively protected.
For Multi-user scenarios , typically each user has its own independent external storage, and the app only has access to the external storage of the current user.
Directory of the Environment API
getdatadirectory (): User Data directory.
getdownloadcachedirectory (): Download cache content directory.
getexternalstoragedirectory (): The primary external storage directory.
However, this directory is probably not currently accessible, such as when the directory is mounted by the user's PC, removed from the device, or other problems occur, and you can get the current status through getexternalstoragestate() .
There are also multiple users or multiple external storage scenarios, and this article is no longer discussed.
In order not to pollute the user's root namespace, the root directory of this external store is not normally used directly.
Any file that is used for private use should be placed in the directory returned by Context.getexternalfilesdir , which will be cleaned up when the application is uninstalled.
Other shared files should be placed in the getExternalStoragePublicDirectory(String)
returned directory.
Write this path requires WRITE_EXTERNAL_STORAGE
permission, read the required READ_EXTERNAL_STORAGE
permissions, of course, write permissions by default contains Read permissions.
starting with KITKAT , Android 4.4 , if your app just needs to store some internal data, consider using:
getExternalFilesDir(String)
or Getexternalcachedir (), they do not need to get permission.
getexternalstoragepublicdirectory (String type) This method takes a parameter that indicates the type of file that the directory is placed in, and the parameter passed in is the directory in the Environment class _xxx static variables, such as DIRECTORY_DCIM.
Note: The passed-in type parameter cannot be null, the returned directory path may not exist, so it must be confirmed before use, such as using File.mkdirs to create the path.
getrootdirectory () get the root directory of Android.
isexternalstorageemulated () if the external memory of the device is simulated with memory, true is returned. (APIlevel One)
isexternalstorageremovable () if the external memory of the device is removable, such as an SD card, it returns TRUE. (API Level 9)
directories in the Context API
Getexternalfilesdir (String type) is the directory on which external storage is applied.
Similar to the getexternalstoragepublicdirectory(String Type) method of the Environment class, Returns a subdirectory that contains the specific type of file specified by the parameter.
Getexternalcachedir () is the cache directory on the external storage that is applied.
from Android 4.4 These two methods do not require read and write permissions, is for the application, if you want to access the relevant directory of other applications, or need to declare read and write permissions.
If the previous version of Android 4.4 was to be accessed or read-write permission, the above two get methods would return NULL if there was no write permission in manifest.
In contrast to the above two methods, the following two methods are used:
getFilesDir()
Getcachedir ()
These two methods get the directory on the memory.
These directories belong to the application, and when the application is unloaded, the contents are removed, but do not rely on the operation of the system.
Test Code
Packagecom.mengdd.utils.android;ImportAndroid.content.Context;Importandroid.os.Environment; Public classDirectoryutils {Private Static FinalString Log_tag = "Directoryutils"; Public Static voidgetenvironmentdirectories () {logutils.i (Log_tag,"Getrootdirectory ():" +environment.getrootdirectory (). toString ()); Logutils.i (Log_tag,"Getdatadirectory ():" +environment.getdatadirectory (). toString ()); Logutils.i (Log_tag,"Getdownloadcachedirectory ():" +environment.getdownloadcachedirectory (). toString ()); Logutils.i (Log_tag,"getExternalStorageDirectory ():" +environment.getexternalstoragedirectory (). toString ()); Logutils.i (Log_tag,"Getexternalstoragepublicdirectory (Environment.directory_pictures):" +environment.getexternalstoragepublicdirectory (environment.directory_pictures). ToStrin g ());//logutils.i (//Log_tag,//"isexternalstorageemulated ():"//+ environment.isexternalstorageemulated ());////logutils.i (//Log_tag,//"isexternalstorageremovable ():"//+ environment.isexternalstorageremovable ()); } Public Static voidgetapplicationdirectories (Context context) {logutils.i (Log_tag,"Context.getfilesdir ():" +Context.getfilesdir (). toString ()); Logutils.i (Log_tag,"Context.getcachedir ():" +Context.getcachedir (). toString ()); //methods below would return NULL if the permissions deniedlogutils.i (Log_tag,"Context.getexternalfilesdir (environment.directory_movies):" +context. Getexternalfilesdir (environment.directory_movies)); Logutils.i (Log_tag,"Context.getexternalcachedir ():" +Context.getexternalcachedir ()); }}
Output Log on Mi 2S:
On the Samsung S5660 (API Level 9, commented out two methods):
References
Android.os.Environment
Http://developer.android.com/reference/android/os/Environment.html
External Storage Technical Information
http://source.android.com/devices/tech/storage/
Context
Http://developer.android.com/reference/android/content/Context.html