How to read multiple files from a resource directory
Sorry for this question, I'm an amateur programmer.
I have 495 text files in raw
my project directory that I want to read. The problem is that I can only read one of them with the following code, but I have no idea how to read all the files.
please help me,
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.");
}
+3
source to share
1 answer
- Read all Raw resource names
-
Get actual id (int) and open resource as 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 } }
0
source to share