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

后端 未结 6 2021
心在旅途
心在旅途 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

    0 讨论(0)
  • 2020-11-30 02:55

    Look at http://developer.android.com/reference/android/content/res/Resources.html You can generally acquire the Resources instance associated with your application with getResources().

    Resources class provides access to http://developer.android.com/reference/android/content/res/AssetManager.html (see to getAssets() method). And finally obtain access to your packaged (apk) files with AssetManager.list() method. Enjoy!

    0 讨论(0)
  • 2020-11-30 02:57

    You can use AssetManager:

    As far as I can remember, you will have list with (just try different paths):

    final String[] allFilesInPackage = getContext().getResources().getAssets().list("");
    
    0 讨论(0)
  • 2020-11-30 03:09

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

    There are no files in res/raw/ on the device. Those are resources. There is no good way to iterate over resources, other than by using reflection to iterate over the static data members of the R.raw class to get the various ID names and values.

    but doesn't really help me find out where the raw folder exists.

    As a folder, it only exists on your development machine. It is not a folder on the device.

    0 讨论(0)
  • 2020-11-30 03:13

    This code will retrieve all the files from 'New Foder' of sdCard.

        File sdCardRoot = Environment.getExternalStorageDirectory();
        File yourDir = new File(sdCardRoot, "New Folder");
        for (File f : yourDir.listFiles()) {
            if (f.isFile())         
            {               
               String name = f.getName();           
               Log.i("file names", name);          
    
            }
    
         }
    

    and also make sure to add android sd card write permission in your manifest.xml file

    0 讨论(0)
  • 2020-11-30 03:17

    From a Context, you can call something like this:

    getResources().getAssets().list("thePath");
    

    link to documentation

    0 讨论(0)
提交回复
热议问题