Two ways for Android to read in-Project resource files

Source: Internet
Author: User

Objective
Welcome everyone I share and recommend useful code Snippets ~ ~
Statement
Welcome reprint, but please keep the original source of the article:
CSDN:
http://www.csdn.net
rainy season o mo away:http://blog.csdn.net/luckkof

Body

Summarize:

Asset Directory Files:

Called native files, which are not compressed when packaged as an apk file and do not automatically generate an ID for r files

Access:

Get path: File://android_asset/test.xml

  1. //The Assets folder in the project root directory, such as Assets/test.xml so we use the following code   
  2. assetmanager am =  this .getresources (). Getasset ();   
  3. //or assetmanager am  = this.getassets ();    
  4. InputStream is = Am.open (Test.xml);

Files in res directory:

The files are mapped to the R.java file

  1. //res/raw directory storage, such as test.xml a binary file, we can read can directly   
  2. InputStream is=context.getresources (). Openrawresource (R.raw.test);

The same points as Res/raw and assets:

1. The files in both directories will be stored intact in the APK package after packaging, will not be compiled into binary, you can put the database file here.

. Res/raw can not have a directory structure, and assets can have a directory structure, that is, assets directory can be set up folder

-----------------------------------------------------------------------------------

In many cases, we need to access the resources files in Android, these resource files are mainly divided into two categories, one for the asset directory, called the native files, such files are packaged as an APK file is not compressed, and the other is the res files, such files in the package as an APK file , it will be small memory optimization oh.

There are two different types of files that correspond to different access modes. Let's take a look at the access to the asset file:

There is a special class in Android that handles the application's access to the asset file, which is Assetmanager. There is an open () method that returns a InputStream object to be used by the user, based on the file name provided by the user.

Here we can see the open method and other corresponding methods, for which there is a parameter accessmode, specifies the system to obtain the resource mode. The main values are as follows:

Access_buffer: Load file into memory, this method is suitable for small file reading;

Access_random: Can read data blocks at random, either forward or backward

Access_straming: Sequential Read content

Access_unknown: When not explicitly specified, use the default mode

Here we must note that the open (String fileName) is read by default using access_streaming mode!!

So how do we get a Assetmanager object that we can get through the following code in the activity:

[Java] view Plaincopy
  1. < Span style= "font-size:10px" >// Assets folder in the project root directory, such as assets/test.xml  so we use the following code &NBSP;&NBSP;
  2. < Span style= "font-size:10px" >assetmanager am =  this .getresources (). Getasset ();   
  3. < Span style= "font-size:10px" >// or assetmanager am = this.getassets ();     &NBSP;&NBSP;
  4. InputStream is = Am.open (Test.xml);

How do I access files under the Res directory?

We can access the activity in the following ways:

[Java] view Plaincopy
  1. < Span style= "font-size:10px" >//res/ Raw directory storage, such as test.xml a binary file, we can read can be directly    &NBSP;&NBSP;
  2. InputStream is=context.getresources (). Openrawresource (R.raw.test);

Finally, we must pay attention to three points:

Files stored under Res/raw will generate IDs accordingly, while files under assets will not generate IDs;

Asset and RES files are read-only and cannot be written;

A single file larger than 1MB can not be processed in Assertmanager, otherwise it will be reported as an exception, and the second raw does not have this limitation.


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.