Android,如何获取文件夹中所有文件的列表?
我需要 res/raw/ 目录中所有文件的名称(字符串)。
我尝试过:
File f = new File("/");
String[] someFiles = f.list();
看起来根目录是android模拟器的根目录......而不是我的电脑根目录。这很有意义,但并不能真正帮助我找出原始文件夹所在的位置。
更新:感谢所有精彩的回复。看来其中一些方法确实有效,但只成功了一半。也许更详细的描述会有所帮助
我想获取原始文件夹中的所有 mp3 文件,以便获取所有名称,然后将它们添加到 URI 以按以下方式播放随机 MP3
String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis";
Uri uri = Uri.parse(uriStr);
singletonMediaPlayer = MediaPlayer.create(c, uri);
...我将“dennis.mp3”放入资产文件夹中,它确实按预期显示,但是,使用上面的代码,我无法再访问该 MP3,除非有以下内容:
String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis";
Uri uri = Uri.parse(uriStr);
I need the name (String) of all files in res/raw/ directory.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要列出原始资产的所有名称(基本上是去掉扩展名的文件名),您可以执行以下操作:
由于实际文件在手机上后不仅仅位于文件系统上,因此名称无关紧要,并且您需要通过分配给该资源名称的整数来引用它们。在上面的示例中,您可以这样获得这个整数:
这与您通过引用 R.raw.whateveryounamedtheresource 获得的 int 相同
To list all the names of your raw assets, which are basically the filenames with the extensions stripped off, you can do this:
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:
This is the same int which you'd get by referring to R.raw.whateveryounamedtheresource
此代码将从 sdCard 的“New Foder”检索所有文件。
并确保在您的manifest.xml文件中添加android sd卡写入权限
This code will retrieve all the files from 'New Foder' of sdCard.
and also make sure to add android sd card write permission in your manifest.xml file
设备上的
res/raw/
中没有文件。这些都是资源。除了使用反射来迭代 R.raw 类的静态数据成员以获取各种 ID 名称和值之外,没有什么好的方法可以迭代资源。作为文件夹,它仅存在于您的开发计算机上。它不是设备上的文件夹。
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 theR.raw
class to get the various ID names and values.As a folder, it only exists on your development machine. It is not a folder on the device.
您可以使用 AssetManager:
据我所知,您将有列表(只需尝试不同的路径):
You can use AssetManager:
As far as I can remember, you will have list with (just try different paths):
看看 http://developer.android.com/reference/android/content /res/Resources.html
您通常可以使用 getResources() 获取与您的应用程序关联的 Resources 实例。
Resources 类提供对 http://developer.android.com/reference/ 的访问android/content/res/AssetManager.html(参见 getAssets() 方法)。
最后使用 AssetManager.list() 方法获取对打包 (apk) 文件的访问权限。
享受!
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!
从
Context
中,您可以调用如下内容:文档链接
From a
Context
, you can call something like this:link to documentation
这里有两个 Kotlin 的解决方案。
方法 1
请注意,要获取非静态属性(实例成员),请使用
memberProperties
并将对象实例传递给get( )
函数调用。方法2
Here are two solutions for Kotlin.
Method 1
Note that, to get properties that are not static (instance members), instead of
staticProperties
above, usememberProperties
and pass your object instance to theget()
function call.Method 2