Android,如何获取文件夹中所有文件的列表?

发布于 2024-11-17 17:32:38 字数 769 浏览 3 评论 0原文

我需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜唯美灬不弃 2024-11-24 17:32:38

要列出原始资产的所有名称(基本上是去掉扩展名的文件名),您可以执行以下操作:

public void listRaw(){
    Field[] fields=R.raw.class.getFields();
    for(int count=0; count < fields.length; count++){
        Log.i("Raw Asset: ", fields[count].getName());
    }
}

由于实际文件在手机上后不仅仅位于文件系统上,因此名称无关紧要,并且您需要通过分配给该资源名称的整数来引用它们。在上面的示例中,您可以这样获得这个整数:

int resourceID=fields[count].getInt(fields[count]);

这与您通过引用 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:

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

寄离 2024-11-24 17:32:38

此代码将从 sdCard 的“New Foder”检索所有文件。

    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);          

        }

     }

并确保在您的manifest.xml文件中添加android sd卡写入权限

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

梦言归人 2024-11-24 17:32:38

我需要 res/raw/ 中所有文件的名称(字符串)

设备上的 res/raw/ 中没有文件。这些都是资源。除了使用反射来迭代 R.raw 类的静态数据成员以获取各种 ID 名称和值之外,没有什么好的方法可以迭代资源。

但并不能真正帮助我找出原始文件夹所在的位置。

作为文件夹,它仅存在于您的开发计算机上。它不是设备上的文件夹。

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.

叫嚣ゝ 2024-11-24 17:32:38

您可以使用 AssetManager

据我所知,您将有列表(只需尝试不同的路径):

final String[] allFilesInPackage = getContext().getResources().getAssets().list("");

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("");
温柔戏命师 2024-11-24 17:32:38

看看 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!

半寸时光 2024-11-24 17:32:38

Context 中,您可以调用如下内容:

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

文档链接

From a Context, you can call something like this:

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

link to documentation

漫漫岁月 2024-11-24 17:32:38

这里有两个 Kotlin 的解决方案。

方法 1

// Make sure to add this dependency in your build file
// implementation(kotlin("reflect"))

for (property in R.raw::class.staticProperties) {
    val itsName = property.name
    val itsValue = property.get() // Aka, the resource ID
}

请注意,要获取非静态属性(实例成员),请使用 memberProperties 并将对象实例传递给 get( ) 函数调用。

方法2

val fields = R.raw::class.java.getFields()
for (field in fields) {
    val itsName = field.name
    val itsValue = field.getInt(field) // Aka, the resource ID
}

Here are two solutions for Kotlin.

Method 1

// Make sure to add this dependency in your build file
// implementation(kotlin("reflect"))

for (property in R.raw::class.staticProperties) {
    val itsName = property.name
    val itsValue = property.get() // Aka, the resource ID
}

Note that, to get properties that are not static (instance members), instead of staticProperties above, use memberProperties and pass your object instance to the get() function call.

Method 2

val fields = R.raw::class.java.getFields()
for (field in fields) {
    val itsName = field.name
    val itsValue = field.getInt(field) // Aka, the resource ID
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文