读书人

读取raw 文件夹上的资源

发布时间: 2012-09-06 10:37:01 作者: rapoo

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

读书人网 >移动开发

热点推荐