lets say i have 100 .mp3 files in raw folder
for a single known file can read files can use getResources().openRawResource(R.raw.myfilename)
but i want to get all the files names in raw folder and then pick a random one from that list
any help?
lets say i have 100 .mp3 files in raw folder
for a single known file can read files can use getResources().openRawResource(R.raw.myfilename)
but i want to get all the files names in raw folder and then pick a random one from that list
any help?
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Since the actual files aren't just sitting on the filesystem once they're on the phone, the name is irrelevant, and you'll need to refer to them by the integer assigned to that resource name. In the above example, you could get this integer thus:
int resourceID=fields[count].getInt(fields[count]);
This is the same int which you'd get by referring to R.raw.whateveryounamedtheresource
Just for completeness, here is the same thing in Kotlin:
package com.kana_tutor.KanaCard
import android.util.Log
import java.lang.reflect.Field
fun listRaw() {
val fields: Array<Field> =
com.kana_tutor.KanaCard.R.raw::class.java.getFields()
fields.forEach {field ->
Log.d("Knames", String.format(
"name=\"%s\", id=0x%08x",
field.name, field.getInt(field))
)
}
}
To add to what @EminemT said, R.raw.class.getFields()
seemed to give internal files used by android. To get the files you put in res/raw, use R.raw.class.getDeclaredFields()
instead.