Android Resource File Categories:
Android resource files can be broadly divided into two types:
The first is stored in the Res directory.
resource files that can be compiled:
This resource file system will automatically generate the ID of the resource file in R.java, so access to this resource file is relatively simple, through the r.xxx.id can be;
The second type is stored under the assets directory.Native Resource File:
Because the system does not compile the resource files under assets , we cannot access them in r.xxx.id way . Can I access them through the absolute path of the resource? Because APK installation will be placed in the/data/app/**.apk directory, in the form of APK, asset/res and is bound to the APK, and will not be extracted into the/data/data/yourapp directory down, So we can't get the absolute path to the assets directly, because they don't have it at all.
Fortunately the Android system provides us with a Assetmanager tool class .
Viewing the official API,Assetmanager provides access to the application's original resource file ; This class provides a low-level API that allows you to open and read the raw resource files that are bound together with the application as a simple byte stream.
Access to the 1.assets folder resource the files in the assets folder are kept in the original file format and need to be read by Assetmanager in the form of a byte stream. 1. First in
activity inside Call Getassets () to get Assetmanager reference。 2. Re-use the Assetmanager
Open (String fileName, int accessmode)The InputStream method specifies the read file and the access mode to get the input stream. 3. Then you read the file with the InputStream of the open file, and remember to InputStream when the read is complete.
Close ()。 4. Call Assetmanager.
Close ()Close the Assetmanager.
It is important to note that files from resources and assets can only be read and cannot be written.
The following is read from the raw file:
PublicString Getfromraw () {Try{InputStreamReader Inputreader=NewInputStreamReader (Getresources (). Openrawresource (R.raw.test1)); BufferedReader Bufreader=NewBufferedReader (Inputreader); String Line=""; String Result=""; while(line = Bufreader.readline ())! =NULL) Result+=Line ; returnResult; } Catch(Exception e) {e.printstacktrace (); } }
The following is read directly from assets:
Publicstring Getfromassets (String fileName) {Try{InputStreamReader Inputreader=NewInputStreamReader (Getresources (). Getassets (). open (FileName); BufferedReader Bufreader=NewBufferedReader (Inputreader); String Line=""; String Result=""; while(line = Bufreader.readline ())! =NULL) Result+=Line ; returnResult; } Catch(Exception e) {e.printstacktrace (); } }
Next, we create a new project file, named Assetsdemo:
Then set up a layout file, such as the next, very simple, without me to introduce more, we can see at a glance:
Then, I found a paragraph of text from the Internet, stored in the assets file directory, named Health.txt This is the file we are going to read today.
this. txt file, we can directly double-click to view. as shown below.
Next, is the play of today, Android reads the core code of the file. Just put the code directly:
Packagecom.assets.cn;ImportJava.io.InputStream;Importorg.apache.http.util.EncodingUtils;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classAssetsdemoactivityextendsActivity { Public Static FinalString ENCODING = "UTF-8"; TextView TV1; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); TV1=(TextView) Findviewbyid (R.ID.TV1); Tv1.settextcolor (Color.Black); Tv1.settextsize (25.0f); Tv1.settext (Getfromassets ("Health.txt"));}//get the file from the Assets folder and read the data Publicstring Getfromassets (String fileName) {string result= ""; Try{InputStream in=getresources (). Getassets (). open (fileName);//Gets the number of bytes in the fileintLenght =in.available ();//Create a byte arraybyte[] buffer =New byte[lenght];//read the data in a file into a byte arrayin.read (buffer); result=encodingutils.getstring (buffer, ENCODING);} Catch(Exception e) {e.printstacktrace ();}returnresult;}}
Here is the Mainfest file:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "com.assets.cn"Android:versioncode= "1"Android:versionname= "1.0"> <USES-SDKandroid:minsdkversion= "8" /> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <ActivityAndroid:name=". Assetsdemoactivity "Android:label= "@string/app_name"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Finally, let's Run the program:
Access to Assets folder resources in Android (Java) Learning notes 135:android