Android save files list of various directories

Source: Internet
Author: User

In general, we can get the directory to save the file through the context and environment

($rootDir)+-/dataenvironment.getdatadirectory ()|   || |($appDataDir)| +-data/Com.srain.cube.sample|       || |($filesDir)| + + files, Context.getfilesdir ()/Context.getfilestreampath ("")|       |       ||       | +-File1-Context.getfilestreampath ("File1")| |($cacheDir)| +-CacheContext.getcachedir ()|       || +-App_$name (Context.getdir (String name,intmode)|($rootDir)+-/storage/sdcard0environment.getexternalstoragedirectory ()| /Environment.getexternalstoragepublicdirectory ("")    | +-Dir1-environment.getexternalstoragepublicdirectory ("Dir1")    | |($appDataDir)+-andorid/data/Com.srain.cube.sample| |($filesDir)+-Context.getexternalfilesdir ("")        |        |   | +-File1-Context.getexternalfilesdir ("File1")        | +-MusicContext.getexternalfilesdir (Environment.music); | +-Picture ... Environment.picture| +- ...        | |($cacheDir)+-CacheContext.getexternalcachedir ()| +- ???
Characteristics of each path

The following describes the characteristics of these paths and the details to be aware of in use:

1. root directory ( $rootDir ):

    • Internal storage path: /data , by Environment.getDataDirectory() getting
    • External storage path: /storage/sdcard0 (also similar to/mnt/), by Environment.getExternalStorageDirectory() obtaining

Example

   

environment.getdatadirectory ():          /dataenvironment.getexternalstoragedirectory ():         /storage/sdcard0

2. Application Data Catalog ( $appDataDir )

    • Internal storage: $appDataDir = $rootDir/data/$packageName ,
    • External storage:$appDataDir = $rootDir/Andorid/data/$packageName

  The data in these directories, after the app is uninstalled, will be deleted by the system, we should put the app's data in both directories.

3. The data directory that is exposed in the external storage. These directories will not be deleted by the system as the app is deleted, please use:

    

environment.getexternalstoragedirectory ():      /storage/sdcard0//  same $rootDirenvironment.getexternalstoragepublicdirectory (""):      /storage/sdcard0environment.getexternalstoragepublicdirectory ("Folder1"):     / Storage/sdcard0/folder1

4. Directory under the Application Data directory

In general, under $appdatadir, there will be two directories:

1. data cache: $cacheDir = $appDataDir/cache :

      • Internal storage: Context.getCacheDir() When the body is out of memory, the file is deleted
      • External storage:Context.getExternalCacheDir()

        External storage without real-time monitoring, when the space is low, the file will not be deleted in real time, may return an empty object

        Example

    Context.getcachedir ():         /data/data/com.srain.cube.sample/Cache    Context.getexternalcachedir ():         /storage/sdcard0/android/data/com.srain.cube.sample/cache        

2. File directory $filesDir = $appDataDir/files :

Internal storage: by Context.getFilesDir() getting

    Context.getFileStreamPath(String name)Returns the name file object as a filename, which is name empty, and returns $filesDir itself

Example:

    

Context.getfilesdir ():          /data/data/com.srain.cube.sample/Filescontext.getfilestreampath (""):        /data/data/ com.srain.cube.sample/Filescontext.getfilestreampath ("File1"):        /data/data/ Com.srain.cube.sample/files/file1

External storage: Passed Context.getExternalFilesDir(String type) as type an empty string when fetched.

    typeThe system specifies several types:

Environment.directory_music environment.directory_pictures ...

Example

  

    Context.getexternalfilesdir (""):         /storage/sdcard0/android/data/com.srain.cube.sample/files    Context.getexternalfilesdir (environment.directory_music)         /storage/sdcard0/android/data/com.srain.cube.sample/files/music

3. $cacheDir / $filesDir Security

In internal storage, the $cacheDir app $filesDir is secure, and other apps can't read the app's data, while the external storage is not.

In external storage, these two folders are also accessible to other applications.

In external storage, $filesDir media files are not scanned as media and added to the Media Library.

4. $cacheDir / $filesDir Sibling Directory

In internal storage: through Context.getDir(String name, int mode) the available and $filesDir /or $cacheDir sibling directories

The directory is named app_ + name by the mode to control whether the directory is for app private or other apps to read and write.

Example:

    Context.getdir ("Dir1", mode_private):        /data/data/com.srain.cube.sample/app_dir1

5. Pay special attention to external storage, access to $cacheDir or $filesDir and below the path

Under API Level 8, or if there is not enough space, the associated method will need to be constructed when the path is empty.

@TargetApi (version_codes. FROYO) Public StaticFile Getexternalcachedir (context context) {if(Build.VERSION.SDK_INT >=Build.version_codes. FROYO)) {File path=Context.getexternalcachedir (); //in some case, even the SD card is mounted,//Getexternalcachedir would return null//May was it nearly full.        if(Path! =NULL) {            returnpath; }    }    //before Froyo or the path is null,//we need to construct the external cache folder ourselves    FinalString Cachedir = "/android/data/" + context.getpackagename () + "/cache/"; return NewFile (Environment.getexternalstoragedirectory (). GetPath () +cachedir);}

Reference: Http://liaohuqiu.net/cn/posts/storage-in-android/

Android save files list of various directories

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.