• 资源文件assets和 res下面raw文件的使用不同点


    在建立项目中一般会默认建立assets文件,当然我们还可以在res文件下面建立raw文件夹,这里面都可以存放一些图片,音频或者文本信息,可以供我们在程序当中进行使用,不过他们两个也有不同点;

          assets下面的文件不会被编译,通过路径可以去访问其中的内容。raw中文件会自动编译,我们可以在R.java文件中找到对应的ID,

        看下面截图:

          

       

           那么既然这样那我们平时该怎么样进行把资源放入这两个文件当中呢?

          我个人平时喜欢比较文件的大小,如果文件比较大一点的会放入到aeests文件中,因为用这个文件文件当中的信息,相当于要去进行IO流操作,比较耗时的操作

          其中比较重要的是获取到Assets和Raw文件夹中的资源方法:

          Assets:AssetManager assetManager = getAssets();

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

          下面该Demo的Activity源代码:

          

    [java] view plaincopy
     
    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.  * 该Demo演示Assets和Raw文件夹中文件的使用方法 
    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.      * 使用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.      * 使用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.      * 进行IO流读写 
    55.      *  
    56.      * @param inputStream 
    57.      * @return oStream.toString() or “文件读写失败” 
    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 "文件读写失败";  
    70.         }  
    71.     }  
    72. }  

         布局文件:

          

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <LinearLayout  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:orientation="horizontal" >  
    11.   
    12.         <TextView  
    13.             android:layout_width="wrap_content"  
    14.             android:layout_height="wrap_content"  
    15.             android:text="@string/et1" />  
    16.   
    17.         <EditText  
    18.             android:id="@+id/et1"  
    19.             android:layout_width="fill_parent"  
    20.             android:layout_height="wrap_content" />  
    21.     </LinearLayout>  
    22.   
    23.     <LinearLayout  
    24.         android:layout_width="fill_parent"  
    25.         android:layout_height="wrap_content"  
    26.         android:orientation="horizontal" >  
    27.   
    28.         <TextView  
    29.             android:layout_width="wrap_content"  
    30.             android:layout_height="wrap_content"  
    31.             android:text="@string/et2" />  
    32.   
    33.         <EditText  
    34.             android:id="@+id/et2"  
    35.             android:layout_width="fill_parent"  
    36.             android:layout_height="wrap_content" />  
    37.     </LinearLayout>  
    38.   
    39. </LinearLayout>  

        Demo运行效果截图:

        

  • 相关阅读:
    javaweb中如何给自己的网站更改ico图标
    基于Zookeeper的分布式锁
    Linux 基础——权限管理命令chmod
    Java--实现单点登录
    Session和Cookie的区别与联系
    selectedIndex返回被选中的option的index.
    onchange事件可以使用于: <input>, <select>, 和 <textarea>。
    js判断字符串长度
    在容器最前面添加DOM元素 parent.insertBefore(new, parent.children[0])
    在容器末尾添加DOM元素 parent.appendChild(newChild)
  • 原文地址:https://www.cnblogs.com/awkflf11/p/5094041.html
Copyright © 2020-2023  润新知