In the establishment of the project will generally default to establish the assets file, of course, we can also create a raw folder under the Res file, where the surface can be stored in some pictures, audio or text information, can be used in the program, but they have different points;
Assets the following files will not be compiled, and the path can be used to access the contents. The files in raw are automatically compiled, and we can find the corresponding IDs in the R.java file.
See below:
So how do we usually put the resources into these two files?
I personally like to compare the size of the file, if the file is larger than the file will be put into the aeests, because the information in this file is equivalent to the IO stream operation, more time-consuming operation
It is more important to obtain the resource methods in the assets and raw folders:
Assets:assetmanager Assetmanager = Getassets ();
Raw:inputstream InputStream = Getresources (). Openrawresource (R.raw.demo);
The following is the demo activity source code:
[Java]View Plaincopy
- Package Com.jiangqq.aeesrtandraw;
- Import Java.io.ByteArrayOutputStream;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import android.app.Activity;
- Import Android.content.res.AssetManager;
- Import Android.os.Bundle;
- Import Android.widget.EditText;
- /**
- * This demo demonstrates how to use files in assets and raw folders
- *
- * @author JIANGQQ
- *
- */
- Public class Aeesrtsandrawactivity extends Activity {
- private EditText et1, Et2;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Readassets ();
- Readraw ();
- }
- /**
- * Use the files in assets
- */
- private void Readassets () {
- Et1 = (EditText) Findviewbyid (R.ID.ET1);
- Assetmanager Assetmanager = Getassets ();
- try {
- InputStream InputStream = Assetmanager.open ("Demo.txt");
- Et1.settext (Read (InputStream));
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- /**
- * Use the files in raw
- */
- private void Readraw () {
- Et2 = (EditText) Findviewbyid (R.id.et2);
- InputStream InputStream = Getresources (). Openrawresource (R.raw.demo);
- Et2.settext (Read (InputStream));
- }
- /**
- * IO Stream read/write
- *
- * @param inputstream
- * @return ostream.tostring () or "file read and write Failed"
- */
- private String Read (InputStream inputstream) {
- try {
- Bytearrayoutputstream OStream = new Bytearrayoutputstream ();
- int length;
- While (length = Inputstream.read ())! =-1) {
- Ostream.write (length);
- }
- return ostream.tostring ();
- } catch (IOException e) {
- return "file read and write failed";
- }
- }
- }
Layout file:
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <linearlayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="Horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/et1" />
- <EditText
- android:id="@+id/et1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </linearlayout>
- <linearlayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="Horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/et2" />
- <EditText
- android:id="@+id/et2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </linearlayout>
- </linearlayout>
Demo Run Effect:
Resource files assets and res use different points below the raw file