鸟语天空
使用静态资源——raw
post by:追风剑情 2015-8-17 20:42

一、放个txt文件到res/raw/textfile.txt

二、创建活动

package com.example.androidtest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class FilesActivity3 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_files_activity3);
		
		//读取res/raw/textfile.txt文件
		InputStream is = this.getResources().openRawResource(R.raw.textfile);
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String str = null;
		try {
			while((str = br.readLine()) != null) {
				Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
			}
			is.close();
			br.close();
		} catch (IOException e) {
			Log.d("AndroidTest", e.getLocalizedMessage());
		}
	}
}

运行效果

raw.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容