Article source: blog.csdn.net/developer_j…

You can also create a raw folder under the RES file, which can store some pictures, audio or text information for use in the program. However, there are some differences between the two files.

Files under assets are not compiled and can be accessed through a path. The raw file is automatically compiled, and we can find the corresponding ID in the r.java file.

Take a look at the screenshot below:

      

   

So in that case how do we normally put resources into these two files?

Personally, I like to compare file sizes. If the files are larger, I put them into AEests because the information in these files is equivalent to doing I/O stream operations, which are time-consuming

The most important method is to obtain Assets from Assets and Raw folders:

      Assets:AssetManager assetManager = getAssets();

      Raw:        InputStream inputStream = getResources().openRawResource(R.raw.demo); 

The following Demo Activity source code:

      

[java]  view plain copy

  1. package com.jiangqq.aeesrtandraw;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.res.AssetManager;  
  9. import android.os.Bundle;  
  10. import android.widget.EditText;  
  11.   
  12. / * *
  13. * This Demo shows how to use files in Assets and Raw folders
  14.  *  
  15.  * @author jiangqq 
  16.  *  
  17. * /
  18. public class AeesrtsAndRawActivity extends Activity {  
  19.     private EditText et1, et2;  
  20.   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.   
  26.         readAssets();  
  27.         readRaw();  
  28.     }  
  29.   
  30. / * *
  31. * Use files within Assets
  32. * /
  33.     private void readAssets() {  
  34.         et1 = (EditText) findViewById(R.id.et1);  
  35.         AssetManager assetManager = getAssets();  
  36.         try {  
  37.             InputStream inputStream = assetManager.open(“demo.txt”);  
  38.             et1.setText(read(inputStream));  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43.   
  44. / * *
  45. * Use files from Raw
  46. * /
  47.     private void readRaw() {  
  48.         et2 = (EditText) findViewById(R.id.et2);  
  49.         InputStream inputStream = getResources().openRawResource(R.raw.demo);  
  50.         et2.setText(read(inputStream));  
  51.     }  
  52.   
  53. / * *
  54. * Read and write IO streams
  55.      *  
  56.      * @param inputStream 
  57. * @return ostream.tostring () or “file read/write failed”
  58. * /
  59.     private String read(InputStream inputStream) {  
  60.   
  61.         try {  
  62.             ByteArrayOutputStream oStream = new ByteArrayOutputStream();  
  63.             int length;  
  64.             while ((length = inputStream.read()) != -1) {  
  65.                 oStream.write(length);  
  66.             }  
  67.             return oStream.toString();  
  68.         } catch (IOException e) {  
  69. Return “file read/write failed “;
  70.         }  
  71.     }  
  72. }  

\

Layout file:

      

[html]  view plain copy

  1. The < LinearLayout XMLNS: android = “schemas.android.com/apk/res/and…
  2.     android:layout_width=”fill_parent”  
  3.     android:layout_height=”fill_parent”  
  4.     android:orientation=”vertical” >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width=”fill_parent”  
  8.         android:layout_height=”wrap_content”  
  9.         android:orientation=”horizontal” >  
  10.   
  11.         <TextView  
  12.             android:layout_width=”wrap_content”  
  13.             android:layout_height=”wrap_content”  
  14.             android:text=”@string/et1″ />  
  15.   
  16.         <EditText  
  17.             android:id=”@+id/et1″  
  18.             android:layout_width=”fill_parent”  
  19.             android:layout_height=”wrap_content” />  
  20.     </LinearLayout>  
  21.   
  22.     <LinearLayout  
  23.         android:layout_width=”fill_parent”  
  24.         android:layout_height=”wrap_content”  
  25.         android:orientation=”horizontal” >  
  26.   
  27.         <TextView  
  28.             android:layout_width=”wrap_content”  
  29.             android:layout_height=”wrap_content”  
  30.             android:text=”@string/et2″ />  
  31.   
  32.         <EditText  
  33.             android:id=”@+id/et2″  
  34.             android:layout_width=”fill_parent”  
  35.             android:layout_height=”wrap_content” />  
  36.     </LinearLayout>  
  37.   
  38. </LinearLayout>  

Screenshot of Demo operation effect:

    

     

That’s OK.

\