android 檔案讀取(assets)

來源:互聯網
上載者:User
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 這就是今天我們要讀取的檔案啦。

這個.txt檔案,我們可以直接雙擊查看。如下所示。
接下來,就是今天的重頭戲,Android讀取檔案的核心代碼。就直接貼代碼了。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>
最後,我們運行一下程式。

源碼:http://files.cnblogs.com/greatverve/AssetsDemo.zip
太忙,抽時間研究下sgf讀寫。

url:http://greatverve.cnblogs.com/archive/2012/03/07/android-assets.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.