使用静态资源——raw

作者:追风剑情 发布于:2015-8-17 20:42 分类:Android

一、放个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

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号