六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 50|回复: 0

读取raw 文件夹下的资源

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-1-15 02:30:36 | 显示全部楼层 |阅读模式
import java.io.IOException;import java.io.InputStream;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.EditText;/** * This example show how to use raw files from /raw folder * @author FaYnaSoft Labs * */public class Main extends Activity {private static String LOG_APP_TAG = "tag";private EditText editField;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        editField = (EditText) findViewById(R.id.textId);        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {InputStream inputStream = null;try {inputStream = getResources().openRawResource(R.raw.hello_world);byte[] reader = new byte[inputStream.available()];while (inputStream.read(reader) != -1) {}editField.setText(new String(reader));editField.setSelection(editField.getText().length());} catch(IOException e) {Log.e(LOG_APP_TAG, e.getMessage());} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {Log.e(LOG_APP_TAG, e.getMessage());}}}}});    }} InputStream inputStream = getResources().openRawResource(R.raw.rawresource);
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表