android文件讀取assets

assets文件夾資源的訪問

       assets文件夾裏面的文件都是保持原始的文件格式,需要用AssetManager以字節流的形式讀取文件。
      1. 先在Activity裏面調用getAssets() 來獲取AssetManager引用。
      2. 再用AssetManager的open(String fileName, int accessMode) 方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。 
      3. 然後就是用已經open file 的inputStream讀取文件,讀取完成後記得inputStream.close() 。
      4.調用AssetManager.close() 關閉AssetManager。

需要注意的是,來自Resources和Assets 中的文件只可以讀取而不能進行寫的操作
以下爲從Raw文件中讀取:
代碼

 
   public String getFromRaw(){ 
            try { 
                InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line="";
                String Result="";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) { 
                e.printStackTrace(); 
            }             
    } 
以下爲直接從assets讀取
代碼
    public String getFromAssets(String fileName){ 
            try { 
                 InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) ); 
                BufferedReader bufReader = new BufferedReader(inputReader);
                String line="";
                String Result="";
                while((line = bufReader.readLine()) != null)
                    Result += line;
                return Result;
            } catch (Exception e) { 
                e.printStackTrace(); 
            }
    } 
當然如果你要得到內存流的話也可以直接返回內存流!
接下來,我們新建一個工程文件,命名爲AssetsDemo。
然後建立一個佈局文件,如下,很簡單,無需我多介紹,大家一看就明白。



然後建立一個佈局文件,如下,很簡單,無需我多介紹,大家一看就明白。











然後呢,我從網上找了段文字,存放在assets文件目錄下,取名爲health.txt 這就是今天我們要讀取的文件啦。


package com.assets.cn;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class AssetsDemoActivity extends Activity {
public static final String ENCODING = "UTF-8";
TextView tv1;
    @Override
    public void onCreate(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"));
}

//從assets 文件夾中獲取文件並讀取數據
public String getFromAssets(String fileName){
   String result = "";
   try {
InputStream in = getResources().getAssets().open(fileName);
//獲取文件的字節數
int lenght = in.available();
//創建byte數組
byte[]  buffer = new byte[lenght];
//將文件中的數據讀到byte數組中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}


這裏是mainfest文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.assets.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AssetsDemoActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章