对不起这个问题,我是一名业余程序员。
我的项目的raw
目录中有495个文本文件,我想阅读。问题是,我只能用以下代码读取其中一个,但我不知道如何读取所有文件。
请帮助我,
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.help);
byte[] b = new byte[in_s.available()];
in_s.read(b);
txtHelp.setText(new String(b));
} catch (Exception e) {
// e.printStackTrace();
txtHelp.setText("Error: can't show help.");
}
答案 0 :(得分:0)
获取实际的id(int)并打开Resource作为InputStream
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.context = getApplicationContext();
setContentView(R.layout.activity_main);
Field[] fields = R.raw.class.getFields();
String[] names = new String[fields.length];
// Step 1: Read the names
for (int i = 0; i < fields.length; i++) {
names[i] = fields[i].getName();
}
// Step 2: Read as InputStream
for (int i = 0 ; i < allStringsNames.length ; i++){
int id = getResources().getIdentifier(names[i] , "raw", getPackageName());
InputStream inputStream = getResources().openRawResource(id);
//Do your stuff with variable inputStream
}
}