Comparison of various storage paths for Android files

Source: Internet
Author: User

1, File cachedir = Context.getcachedir ();

The application internal storage (data file private) file is stored in this path, do not need to request permission, when the application is uninstalled, the files in the directory will be deleted.
It is important to note that the directory of this file is related to the storage location of the application,
When the application is moved to an external storage device, the absolute path of the file is also changed, so it is recommended to use relative paths when the data is stored in this directory.
The biggest difference between this directory and the Getfilesdir () directory is that the files in this directory are automatically deleted when the Android device has less storage space or is not available.
The official recommendation is that more than 1MB files are recommended to be stored in the Getexternalcachedir () directory

2, File filesdir = Context.getfilesdir ();

The application internal storage (data file private) file is stored in this path, do not need to request permission, when the application is uninstalled, the files in the directory will be deleted.
It is important to note that the directory of this file is related to the storage location of the application,
When the application is moved to an external storage device, the absolute path of the file is also changed, so it is recommended that when the data is stored in this directory, the relative path
The system provides access to this path file by: Context.openfileoutput (String,int); Context.openfileinput (String name);

3, File Externalcachedir = Context.getexternalcachedir ();

Apply external storage (data file private, System media files cannot be accessed (for example, a MP3 file is saved through the system's folder management system, cannot be found),
When the application is uninstalled, the files in the directory will be deleted, but there is a difference between this and Getcachedir ():
Only the mobile system using virtual external storage (virtual SD card, now the vast majority of mobile phones, do not have to plug the physical SD card),
Before uninstalling the app, the files in that directory will be automatically deleted, and if it is the physical storage (physical SD card), it won't automatically delete the directory and the files under the directory.
When used, it is necessary to determine the mount state of the external storage (Getexternalstoragestate (File)) and to request read and Write permissions (Read_external_storage, write_external_storage)
Note: You can access files in this directory when other apps have read and write access to the SD card

4, File externalfilesdir = Context.getexternalfilesdir (null);

Apply external storage (data file private, System media files cannot be accessed (for example, a MP3 file is saved through the system's folder management system, cannot be found),
When the application is uninstalled, the files in the directory will be deleted, but there is a difference between this and Getfilesdir ():
Only when the mobile system is using virtual external storage (Virtual SD card),
Before uninstalling the application, the files in this directory will be automatically deleted, and if the physical storage (physical SD card) is not deleted automatically, the directory and the files in the directory
When used, it is necessary to determine the mount state of the external storage (Getexternalstoragestate (File)) and to request read and Write permissions (Read_external_storage, write_external_storage)
Note: You can access files in this directory when other apps have read and write access to the SD card

5, File externalstoragedirectory = Environment.getexternalstoragedirectory ();

Application of external storage space (data file is not private, can be accessed by the system program of the phone (such as MP3 format files, will be retrieved by the mobile system), the same directory of files, all the app is also accessible,)
Note: External storage space may be inaccessible, or it has been removed, or storage space corruption is inaccessible, and so on. The state of the external storage space can be judged by the Getexternalstoragestate () method.
Note: To read and write files in this directory, you need to obtain read and write permissions
The directory of files, this directory is the user to operate a root directory, enter the level two directory can be
Getexternalfilesdirs (String), Getexternalcachedirs (), and Getexternalmediadirs (). These methods
The official recommendation is not to use the directory directly, in order to avoid polluting the user's root namespace, apply private data, should be placed in the Context.getexternalfilesdir directory
Other files that can be shared can be placed in Getexternalstoragepublicdirectory (String). Directory

6. File externalstoragepublicdirectory = environment.getexternalstoragepublicdirectory (Environment.DIRECTORY_ PICTURES);

Application of external storage space (data file is not private, can be accessed by the system program of the phone (such as MP3 format files, will be retrieved by the mobile system), the same directory of files, all the app is also accessible,)
This directory is used to store various types of files in the directory, where users can be categorized to manage different types of files (such as music, pictures, movies, etc.);
Types are as follows: Directory_music, Directory_podcasts, Directory_ringtones, Directory_alarms, Directory_notifications, DIRECTORY _pictures, Directory_movies, Directory_downloads, DIRECTORY_DCIM, or directory_documents

Comparison of various storage paths for Android files

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.