閱讀742 返回首頁    go 微軟 go windows


Android中資源文件assets和res下麵raw文件的使用不同點

在建立項目中一般會默認建立assets文件,當然我們還可以在res文件下麵建立raw文件夾,這裏麵都可以存放一些圖片,音頻或者文本信息,可以供我們在程序當中進行使用,不過他們兩個也有不同點;
assets下麵的文件不會被編譯,通過路徑可以去訪問其中的內容。raw中文件會自動編譯,我們可以在R.java文件中找到對應的ID,
看下麵截圖:
3.gif
2012-1-19 16:42 上傳
下載附件 (14.81 KB)
那麼既然這樣那我們平時該怎麼樣進行把資源放入這兩個文件當中呢?
我個人平時喜歡比較文件的大小,如果文件比較大一點的會放入到aeests文件中,因為用這個文件文件當中的信息,相當於要去進行IO流操作,比較耗時的操作
其中比較重要的是獲取到Assets和Raw文件夾中的資源方法:
Assets:AssetManager assetManager = getAssets();
Raw: InputStream inputStream = getResources().openRawResource(R.raw.demo);
下麵該Demo的Activity源代碼:
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;
 

/**
 
* 該Demo演示Assets和Raw文件夾中文件的使用方法
 
* 
* @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();
 
    }
 

    /**
 
     * 使用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();
 
        }
 
    }
 

    /**
 
     * 使用Raw中的文件
 
     */
 
    private void readRaw() {
 
        et2 = (EditText) findViewById(R.id.et2);
 
        InputStream inputStream = getResources().openRawResource(R.raw.demo);
 
        et2.setText(read(inputStream));
 
    }
 

    /**
 
     * 進行IO流讀寫
 
     * 
     * @param inputStream
 
     * @return oStream.toString() or “文件讀寫失敗”
 
     */
 
    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 "文件讀寫失敗";
 
        }
 
    }
 
}

布局文件:
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:andro
 
    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:
 
            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:
 
            android:layout_width="fill_parent"
 
            android:layout_height="wrap_content" />
 
    </LinearLayout>
 

</LinearLayout>

Demo運行效果截圖:
4.gif
2012-1-19 16:43 上傳
下載附件 (17.68 KB)


最後更新:2017-04-02 17:51:23

  上一篇:go Android圖像開源視圖:SmartImageView
  下一篇:go Android 小項目之--數據存儲【Network】(附源碼)