Internal Storage space
The so-called internal and external storage refers to whether the mobile phone is built in. The built-in storage space of the mobile phone is called internal storage. It cannot be changed once the mobile phone leaves the factory. It is also one of the hardware indicators of the mobile phone, generally, the larger the phone storage space, the more expensive the mobile phone will be. (in many places, it is called the mobile phone memory, but we know that this is not accurate when we do software, memory refers to the storage program, data, and instructions when the mobile phone is running. Here, memory should be referred to as memory for internal storage of the mobile phone, rather than memory in a strict sense ).
Internal storage space is very limited, so it is very valuable, so we should try to avoid using; in addition, it is also the main data storage location of the system itself and system applications, once the internal storage space is exhausted, the mobile phone cannot be used. Therefore, we should avoid using the internal storage space as much as possible. The shared preferences mentioned above and the SQLite database mentioned below are also stored in the internal storage space.
Android is a Linux operating system, so its internal storage space is the "/data/Data" directory for applications and users. Compared with other (external storage), it features stable storage, convenient storage, simple operation, and more secure (because it can control access permissions. Its only drawback is that it is relatively limited and valuable.
Although it is easy to know the data path of the program itself, the data path of all applications is "/data/APP-package-name /", the data used by all programs, such as the libs library and sharedpreferences, are stored in this path. But we 'd better not use this path, or never directly reference it.
There are two main methods to use internal storage: File Operations and folder operations. In either way, the context provides corresponding functions. Using context is not only simple and convenient, but also helps us manage these files, it can also help us control the access permissions of files. First, let's talk about the functions related to file and folder operations in context.
A. Create a file and open it into a file output stream. You need to provide a string as the file name Java code.
- Fileoutputstream output = context. openoutputfile (filename, context. mode_private );
- Output. Write (data); // use output to write whatever you like
- Output. Close ();
Fileoutputstream output = context. openoutputfile (filename, context. mode_private); output. Write (data); // use output to write whatever you likeoutput. Close ();
B. Similarly, to open a file as input, you only need to provide the file name Java code
- Fileinputstream input = context. openinputfile (filename );
- Input. Read ();
- Input. Close ();
Fileinputstream input = context. openinputfile (filename); input. Read (); input. Close ();
C. List all created file Java Codes
- String [] files = context. filelist ();
- For (string file: Files ){
- Log. E (TAG, "file is" + file );
- }
String [] files = context. filelist (); For (string file: Files) {log. E (TAG, "file is" + file );}
D. To delete a file, you must be able to delete it when it can be created. Of course, the interface for deleting the file is also provided. It is also very simple and you only need to provide the file name.
Java code
- If (context. deletefile (filename )){
- Log. E (TAG, "delete file" + filename + "sucessfully");
- } Else {
- Log. E (TAG, "failed to delete file" + filename );
- }
If (context. deletefile (filename) {log. E (TAG, "delete file" + filename + "sucessfully");} else {log. E (TAG, "failed to delete file" + filename );}
E. Obtain the path of the file that has been created. It returns a file object used for operating the path Java code.
- File filedir = context. getfiledir ();
- Log. E (TAG, "filedir" + filedir. getabsolutepath ();
File filedir = context. getfiledir (); log. E (TAG, "filedir" + filedir. getabsolutepath ();
F. To create a directory, You need to input the directory name. It returns a file object using the operation path Java code.
- File workdir = context. getdir (dirname, context. mode_private );
- Log. E (TAG, "workdir" + workdir. getabsolutepath ();
File workdir = context. getdir (dirname, context. mode_private); log. E (TAG, "workdir" + workdir. getabsolutepath ();
G. To view the created file as a file object, you need to input the file name and return the Java code of the file object.
- File store = context. getfilestreampath (filename );
- Log. E (TAG, "Store" + store. Length ());
File store = context. getfilestreampath (filename); log. E (TAG, "Store" + store. Length ());
H. Obtain the cache path. If no parameter is required, the Java code of the file object is returned.
- File cachedir = context. getcachedir ();
- Log. E (TAG, "cachedir" + cachedir. getabsolutepath ());
File cachedir = context. getcachedir (); log. E (TAG, "cachedir" + cachedir. getabsolutepath ());
To sum up the operations related to the file, we can obtain the following three features:
1. File Operations only need to provide a file name to the function, so the program only needs to maintain the file name;
2. You do not need to create file objects and input and output streams by yourself. You can return a file object or input and output stream by providing the file name.
3. All objects returned by the path operation are file objects.
As mentioned above, internal storage space is limited, valuable, secure, and stable. Therefore, it should be used to store important data, such as user information, password and other data that does not need to be shared with other applications. It can also be used to create temporary files, but be sure to delete them in time. In addition, there is a very important feature for internal storage, that is, when an application is uninstalled, all the file data of the application in the internal storage space will be deleted. The reason for the system to do so is that the internal storage is very limited and it must ensure its availability, because once it is fully occupied, the system will no longer work normally.
External Storage external storage space
Let's talk about the external storage space of the mobile phone. Compared with the internal storage space, the external storage space refers to the external storage space that does not exist when the mobile phone leaves the factory. Users can freely add external storage media such as Ts cards when using it, SD card and other flash storage media. These Flash Media are expensive because of the low initial space and low current high capacity. Therefore, almost every mobile phone supporting external storage has a large capacity (greater than or equal to 2 GB) flash card.
Android also supports external storage media. In fact, more specifically, it depends on the external memory card, because for Android systems, if there is no external memory card, many system applications cannot use it, for example, multimedia applications cannot be used. Although Android is very dependent, the external memory card also has its own characteristics. Its biggest advantage is that it has a large storage space. Basically, you can use it without restrictions, and you are not worried about clearing data. For the moment, many programs are using external memory cards, but few programs actively clean up data, so no matter how large your SD card is, it has fewer and fewer available space. Unlike internal storage, when a program is detached, the file data created in the external storage is not cleared. Therefore, the responsibility for clearing the external storage space is lost to the user. He can view the SD card at intervals and immediately delete useless data. The disadvantage of external storage is that it is not very stable. for Android phones, it can be said that it is very unstable, and Flash Memory media itself is prone to problems, and the SD card is in a lot of abnormal states.
Let's talk about how to use external storage and APIs:
A. Check media availability check media availability
As mentioned above, the stability of external storage media is very poor, so before using it, you must first check its availability. If it is available, you can use Java code again.
- Final string state = environment. getexternalstoragestate ();
- If (State. Equals (environment. media_mounted) | & nbsp; State. Equals (environment. media_read_only) {// SD card is ready to us
Final string state = environment. getexternalstoragestate (); If (state. equals (environment. media_mounted) | & nbsp; State. equals (environment. media_read_only) {// SD card is ready to us
B. Get the directory to get the path of the external memory card
In fact, the path of the external memory card is "/mnt/sdcard", so you can access it directly. Considering readability and portability, we recommend that you write the following code in Java:
- File sdcarddir = environment. getexternalstoragedirectory ();
File sdcarddir = environment. getexternalstoragedirectory ();
C. For API 8 or greater, there are some other useful APIs helping to Manager Files And Directories.
If you use API 8 (Android 2.2) or higher, there are several more interfaces in the SDK to operate on External Storage files and paths. We recommend that you use the SD card in a more standard way. For example, create a directory to store the corresponding data, such as music, picture, and video. The application directory is also "/Android/data/package-name/data ". For more information, see the document. Of course, just like a programming specification, this is just a specification. You can leave it alone, but it is recommended to follow the recommendations of the document for readability and portability.
The following summarizes some of the features that should be paid attention to during use and External Storage:
A. The external memory card is not ready for use at any time, so be sure to check its availability before use.
B. the data stored on the external memory card is visible to all applications and users (using filemanager), so the security is not very good, although the document claims that the program private data can be written on the external memory card, but it seems useless. With filemanager, you can still delete or edit files (the filemanager function on the market is very powerful, allowing you to see all the files in the SD card and the files that can be viewed by operations ).
C. Android mobile phones support mounting an external memory card to a PC as a USB flash drive. When the data cable is connected, the SD card becomes a USB flash drive and connected to another operating system. In Android, although some file attributes (hidden, Private, etc.) do not work on PC, users can operate files on the PC at Will (this is mentioned in point 2 ).
D. if you use an external memory card to store data, you must handle extra exceptions: Where to store the data when the external memory card is unavailable; how to synchronize the data when it is available (this is a headache, the feasible method is that users are not allowed to write data when the SD card is unavailable, but the user experience is not very good, but as you know, many applications do this); your data is damaged. Of course, common exceptions should also be considered, such as full space, failure to write, and bad disk sectors.
We can also take a look at common file directories:
Do not forget permission <uses-Permission Android: Name = "android. Permission. write_external_storage"/>
Java code
- Log. I (TAG, "getfilesdir =" + getfilesdir ());
- Log. I (TAG, "getexternalfilesdir =" + getexternalfilesdir ("exter_test"). getabsolutepath ());
- Log. I (TAG, "getdownloadcachedirectory =" + environment. getdownloadcachedirectory (). getabsolutepath ());
- Log. I (TAG, "getdatadirectory =" + environment. getdatadirectory (). getabsolutepath ());
- Logs. I (TAG, "getexternalstoragedirectory =" + environment. getexternalstoragedirectory (). getabsolutepath ());
- Log. I (TAG, "getexternalstoragepublicdirectory =" + environment. getexternalstoragepublicdirectory ("pub_test "));
Log. I (TAG, "getfilesdir =" + getfilesdir (); log. I (TAG, "getexternalfilesdir =" + getexternalfilesdir ("exter_test "). getabsolutepath (); log. I (TAG, "getdownloadcachedirectory =" + environment. getdownloadcachedirectory (). getabsolutepath (); log. I (TAG, "getdatadirectory =" + environment. getdatadirectory (). getabsolutepath (); log. I (TAG, "getexternalstoragedirectory =" + environment. getexternalstoragedirectory (). getabsolutepath (); log. I (TAG, "getexternalstoragepublicdirectory =" + environment. getexternalstoragepublicdirectory ("pub_test "));
The execution result is as follows: