Link: http://blog.csdn.net/wuxianglong/article/details/6335683
In many cases, we need to access the resource files in Android. These resource files are mainly divided into two types, these files are not compressed when they are packaged into APK files; the other is files under res. When these files are packaged into APK files, small memory optimization will be performed.
I,Asset File Access
Android has a special class to process the application's access to the asset file. This class is assetmanager. The open () method returns an inputstream object based on the file name provided by the user.
Public final inputstream open (string filename)
Public final inputstream open (string filename, int accessmode)
Here we can see the open method and other corresponding methods. If there is an accessmode parameter, the system gets the resource. The main values are as follows:
Access_buffer: Loads files to the memory. This method is suitable for reading small files;
Access_random: You can read data blocks either forward or backward.
Access_straming: sequential read content
Access_unknown: the default mode is used when no specific time is specified.
Note that open (string filename) uses the access_streaming mode by default to read data !!
Then, how can we obtain an assetmanager object? In the activity, we can use the followingCodeObtain:
// The assets folder under the project root directory, such as assets/test. in XML format, we use the following code assetmanager AM = This. getresources (). getasset (); // or assetmanager AM = This. getassets (); inputstream is = aM. open (test. XML );
II,Res File Access
// Res/raw directory. For example, if test. XML is a binary file, you can directly read inputstream is = context. getresources (). openrawresource (R. Raw. test );
Finally, we must pay attention to three points:
IDs are generated for files stored in RES/raw. files stored in assets do not generate IDS;
Files under asset and Res can only be read and cannot be written;
In assertmanager, you cannot process a single file that exceeds 1 MB. Otherwise, an exception is reported, but the second type of raw does not have this restriction.