Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I tried:

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

It looks like the root directory is the root of the android emulator...and not my computers root directory. That makes enough sense, but doesn't really help me find out where the raw folder exists.

Update: Thanks for all the great replies. It appears that some of these are working, but only getting me half way. Perhaps a more detailed description will aid

I want to get all the mp3 files in the raw folder so I can get all the names, then add them to a URI to play a random MP3 in the following way...

String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis"; Uri uri = Uri.parse(uriStr); singletonMediaPlayer = MediaPlayer.create(c, uri); 

When I put "dennis.mp3" into the assets folder, it does show up as expected, however, using the above code, I can't access that MP3 anymore, unless there is something along the line of:

String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis"; Uri uri = Uri.parse(uriStr); 
like image 808
jasonsemko Avatar asked Jun 30 '11 18:06

jasonsemko


People also ask

How do I get a list of files?

Start -> Run -> Type in “cmd” This will open the command window. Next I will have to move into the correct directory. On my computer, the default directory is on the C: drive, but the folder I want to list the files for is on the D: drive, the first thing I will see is the prompt “C:\>”.

How do I list files in internal storage on Android?

Head to Settings > Storage > Other and you'll have a full list of all the files and folders on your internal storage. (If you'd prefer this file manager be more easily accessible, the Marshmallow File Manager app will add it as an icon to your home screen.)


1 Answers

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

like image 182
Captain Blammo Avatar answered Oct 05 '22 11:10

Captain Blammo