Android:如何检测assets文件夹中的目录?
我正在检索这样的文件
String[] files = assetFiles.list("EngagiaDroid");
我们如何知道它是文件还是目录?
我想循环访问资产文件夹中的目录,然后复制其所有内容。
I'm retrieving files like this
String[] files = assetFiles.list("EngagiaDroid");
How can we know whether it is a file or is a directory?
I want to loop through the directories in the assets folder then copy all of its contents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为更通用的解决方案(如果您有子文件夹等)将是这样的(基于您链接到的解决方案,我也将其添加到那里)
......
:
I think a more general solution (in case you have subfolders etc.) would be something like this (based on the solution you linked to, I've added it there too):
...
...
您可以使用AssetManager的列表方法。
asset 中的任何目录都应至少有一个文件,构建应用程序时将忽略空目录。
因此,要确定某个路径是否是目录,请像这样使用:
You may use list method of AssetManager.
Any directory in asset should have one file at least, empty directory will be ignored when building your application.
So, to determine if some path is directory, use like this:
我发现了这个变体:
它比 .list() 更快
I've discovered this variant:
It's a more faster as .list()
令人震惊的事实是,尽管近 10 年前就有人提出这个问题,但还没有一种简单、优雅、广受好评的方法来确定 AssetManager.list() 返回的数组中的元素是文件还是目录迄今为止任何答案都已提供。
因此,例如,如果资产目录包含一千个元素,那么似乎需要一千个 I/O 操作来隔离这些目录。
对于任何元素,也不存在任何获取其父目录的本机方法 - 对于资产浏览器/选择器等复杂的东西至关重要 - 您最终可能会看到一些非常丑陋的代码。
对我有用的横向方法是假设名称中没有点 (.) 的任何元素都是一个目录。如果后来证明该假设是错误的,则可以轻松纠正。
资源文件通常之所以存在,是因为您将它们放在那里。部署区分目录和文件的命名约定。
The appalling truth is that despite being asked nearly 10 years ago, no simple, elegant, roundly applauded method of determining whether an element in the array returned by AssetManager.list() is a file or a directory has been offered by any answer to date.
So, for example, if an asset directory contains a thousand elements, then seemingly a thousand I/O operations are necessary to isolate the directories.
Nor, for any element, does any native method exist for obtaining its parent directory - vital for something complex like an assets Browser / Picker - where you could end up looking at some seriously ugly code.
The lateral approach that worked for me was to assume that any element without a dot (.) in its name was a directory. If the assumption is later proved wrong it can be easily rectified.
Asset files generally exist because you put them there. Deploy naming conventions that distinguish between directories and files.
另一种依赖异常的方法:
然后调用 checkAssets("someFolder", getAssets()); 或 checkAssets("", getAssets() );如果您想检查根资源文件夹。但请注意,根资产文件夹还包含其他目录/文件(例如 webkit、图像等)
Another way relying on exceptions:
and then just call checkAssets("someFolder", getAssets()); or checkAssets("", getAssets()); if you want to check the root assets folder. But be aware that the root assets folder contains also other directories/files (Eg. webkit, images, etc.)
在您的特定情况下,由于您通过
list
检索了文件,因此您已经知道这些名称存在。这使问题简化了很多。你可以简单地使用这个:另一方面,如果你需要一个通用的解决方案来检测某个路径是否存在并且是一个文件夹,你可以使用这个:
我为网络服务器编写了这些,所以我无法做出假设关于输入路径的形状。但如果您设置了一些规则,则可以稍微简化。一旦确定资产类型,此代码就会立即返回,以避免额外的处理开销。
In your particular case, since you retrieved the files through
list
, you already know that these names exist. This simplifies the problem a lot. You can simply use this:On the other hand, if you need a generic solution to detect if a certain path both exists and is a folder, you can use this:
I wrote these for a web server, so I couldn't make assumptions about the shape of the input path. But it can be simplified a bit if you have some rules set. This code returns immediately once it becomes certain of the type of the asset, to avoid the extra processing overhead.
你也可以尝试这个,它对我有用,因为你不能仅仅依赖 .list()
You can also try this, it works for me, since you cannot rely solely on .list()
您可以从 Android 文件 开始
You can start from Android File
您可以使用 http://developer.android 检查文件是否代表目录.com/reference/java/io/File.html#isDirectory()。你是这个意思吗?
You can check if a File represents a directory using http://developer.android.com/reference/java/io/File.html#isDirectory(). Is that what you mean?