码迷,mamicode.com
首页 > 移动开发 > 详细

Android读取assert文件夹下文件的内容

时间:2015-09-20 22:14:34      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

示例:取资源文件显示在ScrollView当中。

1.ReadAsset.java

package com.example.ReadAsset;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.TextView;  
import java.io.IOException;  
import java.io.InputStream;  
  
public class ReadAsset extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.read_asset);  
  
        try {  
//Return an AssetManager instance for your application‘s package  
            InputStream is = getAssets().open("index.txt");  
            int size = is.available();  
  
            // Read the entire asset into a local byte buffer.  
            byte[] buffer = new byte[size];  
            is.read(buffer);  
            is.close();  
  
            // Convert the buffer into a string.  
            String text = new String(buffer, "GB2312");  
  
            // Finally stick the string into the text view.  
            TextView tv = (TextView) findViewById(R.id.text);  
            tv.setText(text);  
        } catch (IOException e) {  
            // Should never happen!  
            throw new RuntimeException(e);  
        }  
    }  
}  

2. read_asset.xml

<?xml version="1.0" encoding="utf-8"?>  
<ScrollView android:layout_width="fill_parent"  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent" android:paddingTop="50dip">  
    <TextView android:id="@+id/text" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:textStyle="normal" />  
</ScrollView>  

3.然后在工程里面新建一个assets文件夹,随便放一个index.txt的文件在其中,运行 

Android读取assert文件夹下文件的内容

标签:

原文地址:http://www.cnblogs.com/Joanna-Yan/p/4824456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!