首页 > 移动平台 > 详细

android从asset文件夹读取文件

时间:2014-02-08 15:12:52      阅读:634      评论:0      收藏:0      [点我收藏+]

1)将一个txt文本(msg.txt)复制到开发目录的asset文件夹下。

bubuko.com,布布扣

2)用getAssets().open()可以得到一个输入流。注意getAssets方法必须用在Activity下边。如果不是一个activity而只是一个普通class,则要将context传递到class里,然后再用getAssets()。

bubuko.com,布布扣
public myClass(Context myContext) {
    AssetManager mngr = myContext.getAssets();
    InputStream is = mngr.open("textdb.txt");
}
bubuko.com,布布扣

3)得到inputstream可以做自己想做的事了。比如转化成string然后改变textview。

bubuko.com,布布扣
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv= (TextView)findViewById(R.id.textid);
        
        InputStream input;
        try{
            input= getAssets().open("msg.txt");
            int size= input.available();
            byte[] buffer= new byte[size];
            input.read(buffer);
            input.close();
            String text = new String(buffer);
            tv.setText(text);
        }catch(IOException e){
            e.printStackTrace();
        }
        
        
        
        
    }
bubuko.com,布布扣

android从asset文件夹读取文件

原文:http://www.cnblogs.com/miaoz/p/3540071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!