Android _ two methods for reading embedded project resource files

Source: Internet
Author: User

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.

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.