0

I have a number of files stored under res/raw. E.g.:

yeh_vidhi_mangal.txt  
om_jai_mahavir.txt  
tumse_laagi_lagan.txt

I would like to iterate over these files in code. Something like:

Resources myResources = getResources();
for(int i = 0; i < ...;i++)
{
   int id_of_ith_resource = ....
    InputStream in = myResources.openRawResource(id_of_ith_resource);
   // do my stuff
}

How to fill in the dots (...) above?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
morpheus
  • 18,676
  • 24
  • 96
  • 159

2 Answers2

1

if you place your files into assets directory instead of the raw directory you can then use AssetManager.list() method to enumerate them. Not sure why you need them in raw directory as opposed to assets directory.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • One example would be to start use the Network security config (https://developer.android.com/training/articles/security-config.html) where you would need to declare the certificates in the manifest (needs to be raw). But as this feature is not available on all API versions you would also need to iterate over trusted CA anchors programmatically. Related: https://stackoverflow.com/questions/52691058/how-to-add-android-network-security-config-for-api-less-than-24 – Alix Aug 05 '19 at 07:16
0

if your raw folder contain static files only means, files does not generate dynamically then use this method,

int num[] = {R.raw.yeh_vidhi_manga,R.raw.om_jai_mahavir,R.raw.tumse_laagi_lagan};

for(int resId in num){

InputStream ins = getResources().openRawResource(resId);
//do the stuff
}

else if content are generated dynamically from remote folder then use AssetManager and for iterate file AssetManager.list();

Karthi
  • 13,624
  • 10
  • 53
  • 76