首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

读取raw 文件夹上的资源

2012-09-06 
读取raw 文件夹下的资源import java.io.IOExceptionimport java.io.InputStreamimport android.app.Acti

读取raw 文件夹下的资源

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);

热点排行