Transferred from: Http://www.liaohuqiu.net/cn/posts/storage-in-android/
The Android system itself has its own storage, and the SD card can be used to expand the storage space. The former is like the hard disk in PC, the latter is good to move hard. The former space is small, the latter space is large, but the latter is not necessarily available. These issues may be encountered when developing applications that handle local data access:
- Need to determine whether the SD card is available: Occupy too much internal storage of the fuselage, easy to incur user antipathy, priority to store data in the SD card;
Application data storage path, should be consistent with other apps, erase data when the app unloads:
- Unconventional in the SD card root directory to create a directory, causing users to resent
- Users are disgusted with residual directories or data on the user's machine after uninstalling the application
Need to judge the free space between the two: when the SD card exists, the available space is smaller than the internal storage of the fuselage, then the body storage should be selected;
Data security, the application data is not willing to be read and written by other applications;
Image cache, etc., should not be scanned into the user album and other Media library.
-
Starting with API 19/andorid 4.4/kitkat, you no longer need to explicitly declare these two permissions unless you want to read and write application data from other apps ( $appDataDir
)
To determine the SD card available:
/** * Check if the primary "external" storage device is available. * * @return */public static boolean hasSDCardMounted() { String state = Environment.getExternalStorageState(); if (state != null && state.equals(Environment.MEDIA_MOUNTED)) { return true; } else { return false; }}
Usage of storage
Basic operations
To use external storage, the required permissions are in AndoridManifest.xml
:
<uses-permission android:name= "android.permission.WRITE_ External_storage "/><uses-permission android:name=" Android.permission.READ_EXTERNAL_STORAGE "/>
-
At API Level 9 and above, File
the object's getFreeSpace()
method obtains the system root user free space;
getUsableSpace()
Take non-root user free space
Get disk usage When multiple stores are available, and choose the right storage based on current system conditions.
According to the system storage usage, reasonably set the size of the space used by the app, and can also do dynamic adjustment when running.
In the API level 9 and above the system, you can directly invoke File
the relevant methods of the object, the following self-calculation:
@TargetApi(VERSION_CODES.GINGERBREAD)public static long getUsableSpace(File path) { if (path == null) { return -1; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { return path.getUsableSpace(); } else { if (!path.exists()) { return 0; } else { final StatFs stats = new StatFs(path.getPath()); return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks(); } }}
The law of the pathGenerally, the Context
path to Environment
file access is obtained through and related methods.
With these two classes, you get a variety of paths,
($rootDir) +-/data environment.getdatadirectory ()| || | ($appDataDir) | +-data/com.srain.cube.sample| || | ($filesDir) | +-Files, Context.getfilesdir ()/Context.getfilestreampath ("") | | || | +-File1-Context.getfilestreampath ("File1") | | ($cacheDir) | +-Cache-Context.getcachedir () | || +-App_$name (Context.getdir (String name, int mode) | | ($rootDir) +-/storage/sdcard0-environment.getexternalstoragedirectory () | /Environment.getexternalstoragepublicdirectory ("") | +-Dir1-environment.getexternalstoragepublicdirectory ("Dir1") | | ($appDataDir) +-Andorid/data/com.srain.cube.sample | | ($filesDir) +-Context.getexternalfilesdir ("") | |
| +- file1 -> Context.getExternalFilesDir("file1") | +- Music -> Context.getExternalFilesDir(Environment.Music); | +- Picture -> ... Environment.Picture | +- ... | | ($cacheDir) +- cache -> Context.getExternalCacheDir() | +- ???
Characteristics of each pathThe following describes the characteristics of these paths and the details to be aware of in use:
root directory ( $rootDir
):
Android Storage Summary