Android, how do can I get a list of all files in a folder?

后端 未结 6 2045
心在旅途
心在旅途 2020-11-30 02:28

I need the name (String) of all files in res/raw/

I tried:

File f = new File(\"/\"); 
String[] someFiles = f.list();

It looks like

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 02:54

    To list all the names of your raw assets, which are basically the filenames with the extensions stripped off, you can do this:

    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

提交回复
热议问题