繁体   English   中英

如何从资源目录中读取多个文件

[英]How to read multiple files from a resource directory

对不起这个问题,我是一名业余程序员。

我的项目的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.");
}
  1. 阅读所有Raw资源名称
  2. 获取实际的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 } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM