Source: http://blog.csdn.net/zhaokaiqiang1992
As long as the app needs to be networked to get data, the cache file is generated locally, whether it's a version update or a picture cache. So where exactly do these cache files fit in? Does the system provide us with a recommended cache location? What is the difference between different cache locations? Today this article is mainly to illustrate this problem.
First of all, we need to know that in the Android phone, the location of the cache is divided into two categories, one is internal Storage, that is, internal storage, the other is external Storage, that is, external storage. Compared to the old mobile phone, there is a mobile phone internal storage, there is an SD card storage, is corresponding to the two storage location, because the previous SD card can be extended, can be disassembled, so it is possible to use removable as a classification standard for internal and external storage. But now the latest equipment, such as millet, hammers, Huawei, and so on, have canceled the removable SD card, directly welded together with the fuselage, divided into 16G, 32G version, so now the internal and external storage classification is no longer to be detachable as a standard, but with the following aspects as a new standard:
Internal storage:
is always available
The files here are only accessible by your app.
When the user uninstalls your app, the system will clean up all the relevant files inside the internal.
Internal is the best storage area you want to ensure is not accessed by users and other apps.
External storage:
is not always available because the user can choose to use this as a USB storage mode, so it is not accessible.
is accessible to everyone, so the files saved here are lost access control permissions.
When the user uninstalls your app, the system simply deletes the relevant files under the external root directory (Getexternalfilesdir ()).
External is the best storage area where you don't need strict access rights and you want these files to be shared by other apps or to allow users to access them from your computer.
Read internal storage does not require permissions, but read or write to external storage requires permission, in the current version, read permissions are not declared, can also be read, but in the future version may be modified, so be sure to add, if the application requires write permission, then only the Write permission, you do not need to declare Read permission.
The following shows how to get the file location and the difference between inside and outside storage.
I. How to save to internal storage
1.getFileDir () This method allows you to get the files stored inside your app with the path/data/data/pacgage_name/files
We test directly on the code:
[Java]View Plain
- File File1 = new File (Getfilesdir (), "GetFilesDir.txt");
- LOG.D ("TAG", "file1=" + File1.getabsolutepath ());
- try {
- OutputStream outputStream1 = new FileOutputStream (FILE1);
- Outputstream1.write ("file". GetBytes ());
- Outputstream1.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
The results of the operation are as follows:
[HTML]View Plain
- 02-03 07:18:04.068 22237-22237/? D/tag: file1=/data/data/com.socks.baidudemo/files/getfilesdir.txt
2.getCacheDir () This method allows you to get the files stored inside your app with the path/data/data/package_name/cache
Test code:
[Java]View Plain
- File File2 = new File (Getcachedir (), "Cache.txt");
- LOG.D ("TAG", "file2=" + File2.getabsolutepath ());
- try {
- OutputStream outputStream1 = new FileOutputStream (file2);
- Outputstream1.write ("cache". GetBytes ());
- Outputstream1.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
The results of the operation are as follows:
[HTML]View Plain
- 02-03 07:19:31.508 23652-23652/? D/tag: file2=/data/data/com.socks.baidudemo/cache/cache.txt
3.openFileOutput () By this method, we can get to an output stream where the save path of the output stream is/data/data/package_name/files, and the path of the Getfiledir () is consistent
The test code is as follows
[Java]View Plain
- try {
- OutputStream outputStream = Openfileoutput (
- outputstream.write ( Span class= "string" > "Openfileoutput". GetBytes ());
- outputstream.close ();
- } catch (exception e) {
- E.printstacktrace ();
- }
Operation Result:
The internal storage directory for your app is stored in the specific directory of your Android file system as the package name of your app [data/data/com.example.xx]. Technically, if you set the file to be readable, then other apps can read your internal file. However, other apps need to know your package name and file name. If you are not set to be readable or writable, there is no way for other apps to read and write. So as long as you use Mode_private, these files will not be accessible by other apps.
Another thing to remember is that the internal storage is deleted when your app is uninstalled, so we can place our image cache in the cache directory, and the difference between the cache and files is that if the phone's internal storage control is not enough, it chooses the cache directory to delete it, so , do not put the important files in the cache file, can be placed inside files, because this file will only be deleted when the app is uninstalled. It is also important to note that if the application is an update operation, the internal storage will not be deleted, which differs from being manually uninstalled by the user.
Two. How to store externally
1. Status of external storage
Unlike internal storage, the capacity of the external storage is generally large, and when the mobile device is connected to the PC, if we turn on the USB mode to connect with the PC and manipulate the file, the external storage is in the uninstall state, the app cannot operate the file inside, so, Please check the status of the external storage before our app's external store operation.
[Java]View Plain
- / * Checks If external storage is available for read and write * /
- Public Boolean isexternalstoragewritable () {
- String state = Environment.getexternalstoragestate ();
- if (Environment.MEDIA_MOUNTED.equals (state)) {
- return true;
- }
- return false;
- }
- /* Checks If external storage is available to at least read */
- Public Boolean isexternalstoragereadable () {
- String state = Environment.getexternalstoragestate ();
- if (Environment.MEDIA_MOUNTED.equals (state) | |
- Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) {
- return true;
- }
- return false;
- }
2. External private Storage
From the introduction of the above internal storage, the internal storage of files should belong to private files, other apps want to access is more difficult, then the external storage? External storage because of the large capacity, is generally our app to save large files of choice, then is not the external storage inside the file, all the app can be free to access it? This is obviously not the case, in external storage, there is also the concept of private files.
Just as we get the method of internal storage in front of us, we use Context.getexternalcachedir () and Context.getexternalfilesdir () to get the private file to the external storage, we take the following code as an example
[Java]View Plain
- File File3 = new File (Getexternalcachedir (). GetAbsolutePath (), "GetExternalCacheDir.txt");
- try {
- OutputStream outputStream1 = new FileOutputStream (FILE3);
- Outputstream1.write ("Getexternalcachedir". GetBytes ());
- Outputstream1.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- LOG.D ("TAG", "file3=" + file3);
- File File4 = new File (Getexternalfilesdir (environment.directory_pictures), "GetExternalFilesDir.txt");
- try {
- OutputStream outputStream1 = new FileOutputStream (FILE4);
- Outputstream1.write ("Getexternalfilesdir". GetBytes ());
- Outputstream1.close ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
The results of the operation are as follows:
[HTML]View Plain
- 02-03 08:11:38.860 9096-9096/? D/tag: file3=/storage/emulated/0/android/data/com.socks.baidudemo/cache/getexternalcachedir.txt
- 02-03 08:11:38.860 9096-9096/? D/tag: File4=/storage/emulated/0/android/data/com.socks.baidudemo/files/pictures/getexternalfilesdir.txt
The position in the system is as follows
As can be seen, the address of the private file we created is/sdcard/android/date/package_name below, the Android folder is a hidden folder and the user cannot manipulate it.
If we want to cache images and other space-consuming files, it is recommended to put in the Getexternalcachedir () under the file, this file and Getcachedir () very much like, can put cache files, when the app is uninstalled, will be deleted by the system, And the cached content is relatively private to other apps.
There are, however, some differences:
Context.getexternalfilesdir () and Context.getfilesdir () are also different, but they are also deleted when the application is uninstalled.
3. External Public Storage
If your app produces files that don't need to be hidden, which is visible to the user, then you can put the file under the external public store file.
We can get to the common store directory through the following code
[Java]View Plain
- Environment.getexternalstoragedirectory ()
- Environment.getexternalstoragepublicdirectory ()
This method is not the context method, but the environment two methods, the first method obtains is actually the external storage root directory, and the second method obtains is the external storage public directory. In fact, there is no difference in access rights, the difference is getexternalstoragepublicdirectory () at run time, you will need to have a specific parameter to specify these public file types, in order to facilitate the classification with other public files. parameter types include Directory_music or directory_pictures. As follows:
[Java]View Plain
- Public File Getalbumstoragedir (context context, String Albumname) {
- //Get the directory for the app's private pictures directory.
- File File = new file (Context.getexternalfilesdir (
- environment.directory_pictures), albumname);
- if (!file.mkdirs ()) {
- LOG.E (Log_tag, "Directory not created");
- }
- return file;
- }
Whether you're using getexternalstoragepublicdirectory () to store files that you can share, or if you're using Getexternalfilesdir () to store files that are private to your app, it's important to That's the constant you want to use for those APIs like Directory_pictures. Those directory type parameters can ensure that those files are treated correctly by the system. For example, files saved in the Directory_ringtones type are considered ringtone rather than music by the system's media scanner.
Here are the folders for these parameters
Before the cache folder has been a mess, after so have been sorted out, feel a lot, Meng Meng ~ ~ ~ ^_^ ~ ~ ~
----------------------------------------Gorgeous split-line------------------------------------------
Thank you for the comment inside of the Buddy remind, indeed, 360 this software also knows where our cache, so a key to accelerate what, it is possible to kill our cache files, then what to do? We can create a cache folder in the external storage, used to store our image cache what, so that 360 will not be anything to delete, because if the wrong, he is in trouble, haha ha!
Where exactly should the app's cache file exist? You should have made yourself clear after reading this article.