Java 使用 AssetManager.list 列出子目录中的资产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3631370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List assets in a subdirectory using AssetManager.list
提问by Andrew Shelansky
My application has an assets directory in which I've dumped a bunch of text files I need to load at runtime.
我的应用程序有一个资产目录,我在其中转储了一堆需要在运行时加载的文本文件。
I have a directory full of assets of a particular type (i.e., "assets/subdir") and I want to load all of the files in this directory, one at a time.
我有一个包含特定类型资产(即“资产/子目录”)的目录,我想一次加载该目录中的所有文件。
I have code like this:
我有这样的代码:
AssetManager assetMgr = getAssets();
String[] assetsIWant = assetMgr.list("subdir");
for(String asset: assetsIWant) {
doAssetyThing(asset);
}
I've tried a zillion different versions of the parameter to assetMgr.list() and am not getting anywhere.
我已经尝试了无数个不同版本的参数到 assetMgr.list() 并且没有得到任何地方。
If I use "/", I get back a list containing the "assets" directory, and a few random other items (META_INF, for example). If I pass any other string (like "assets" or "assets/" or "/assets" or "/assets/" or "mysubdir" or "/mysubdir" or "assets/mysubdir" or ...) then I get back an empty array.
如果我使用“/”,我会得到一个包含“assets”目录的列表,以及一些随机的其他项目(例如 META_INF)。如果我传递任何其他字符串(如“assets”或“assets/”或“/assets”或“/assets/”或“mysubdir”或“/mysubdir”或“assets/mysubdir”或...),那么我得到返回一个空数组。
The documentation is unfortunately fairly incoherent.
不幸的是,文档相当不连贯。
Does anybody know what the correct formula for that list() parameter is?
有人知道那个 list() 参数的正确公式是什么吗?
回答by Hostile
I'm not sure why it works this way, but when I list "/"
, I get root level stuff, not things from my "assets"
directory. I actually found no way to properly list my assets folder in the short time I spent trying.
我不确定为什么它会这样工作,但是当我列出时"/"
,我得到的是根级别的东西,而不是我"assets"
目录中的东西。我实际上发现没有办法在我尝试的短时间内正确列出我的资产文件夹。
The workaround I'm using is to create a "subdir"
directory inside of my "assets"
directory. I put all the assets that I want to access in this "subdir"
, and do:
我使用的解决方法是"subdir"
在我的"assets"
目录中创建一个目录。我把我想访问的所有资产都放在这个里面"subdir"
,然后做:
String[] assetsIWant = assetMgr.list("subdir");
String[] assetsIWant = assetMgr.list("subdir");
I don't know why "/"
lists the "assets"
directory itself as one of it's files, yet you don't have to specify the "assets"
directory when giving list
a path. Maybe someone else knows, but I hope this tip will help you out anyway.
我不知道为什么"/"
将"assets"
目录本身列为它的文件之一,但是"assets"
在提供list
路径时您不必指定目录。也许其他人知道,但我希望这个技巧无论如何都能帮到你。
回答by ddlw42
Passing an empty string seems to work for me. I get a list of the files in my assets directory when I do the following:
传递一个空字符串似乎对我有用。当我执行以下操作时,我会得到资产目录中的文件列表:
AssetManager aMan = appContext.getAssets();
String[] filelist = aMan.list("");
回答by David-mu
I ve use the following code to list the file name in assets/myFolder/:
我使用以下代码列出 assets/myFolder/ 中的文件名:
String[] fileNames =getAssets().list("myFolder");
for(String name:fileNames){
System.out.println(name);
}
note that the parameter in the method list does not contains "/".
注意方法列表中的参数不包含“/”。
回答by Robin
When you need to have access to a folder down deeper in your hierarchy use
当您需要访问层次结构中更深的文件夹时,请使用
String[] fileNames =getAssets().list("myFolder"+File.separator+"mysubfolder");
instead of "/" inside the String, which would give you an empty String array as result.
而不是 String 中的“/”,这会给你一个空的 String 数组作为结果。
回答by Steven_BDawg
Let's say you have this folder set up: Assets->SubDir1
. You have things in Subdir1
like A.txt
, B.txt
, C.txt
. So from the Project's directory, it would be Assets/SubDir1/A.txt
.
假设您已设置此文件夹:Assets->SubDir1
. 你有Subdir1
类似A.txt
, B.txt
, 的东西C.txt
。因此,从项目的目录中,它将是Assets/SubDir1/A.txt
.
Taking that into account, the way to access that file is similar to what you're doing, adding one thing. That one thing is in your doAssetyThing
function. You have to put the SubDir1
directory in front of it, even if you did a filter of "SubDir1"
in the .list("");
考虑到这一点,访问该文件的方式与您正在执行的操作类似,只是添加了一件事。那一件事在你的doAssetyThing
职能中。你必须把SubDir1
目录放在它前面,即使你"SubDir1"
在.list("");
Example:
例子:
AssetManager am = getResources().getAssets();
String [] list = am.list("SubDir1");
for (String s:list) {
doAssetyThing("SubDir1/" + s);
//I use this next line as the start of copying a pdf from one location
//to another. I wanted to give you a real function in use.
InputStream inStream = am.open("SubDir1/" + s);
//more code
}
I hope this is clear and helps you out!
我希望这很清楚并能帮助你!
回答by Matt
There are a lot of similar questions around this topic, I suspect you are using the assets folder in a library project. if you try to access assets in a library project on android you will get empty contents and the exact symptoms you have described here
关于这个主题有很多类似的问题,我怀疑您正在使用库项目中的资产文件夹。如果您尝试在 android 上访问库项目中的资产,您将获得空内容和您在此处描述的确切症状
from the android "projects" documentation
来自android“项目”文档
source : http://developer.android.com/tools/projects/index.html
来源:http: //developer.android.com/tools/projects/index.html
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
库项目不能包含原始资产
这些工具不支持在库项目中使用原始资产文件(保存在 assets/ 目录中)。应用程序使用的任何资产资源都必须存储在应用程序项目本身的 assets/ 目录中。但是,支持保存在 res/ 目录中的资源文件。
if this is the case, the answer is that you should only include Raw assets in the assets folder of your application project NOTin any project marked as a library (you can check this by right clicking your project and selecting properties, select the android entry on the left and look at the is library checkbox)
如果是这种情况,答案是您应该只在应用程序项目的资产文件夹中包含原始资产,而不是在任何标记为库的项目中(您可以通过右键单击您的项目并选择属性来检查这一点,选择 android 条目在左侧,查看是库复选框)
回答by Andro
This solution seems working for me:
Keep a copy of each file you want to access from a sub directory in the assets directory too.
Use same convention. i.e. list("subdirectory")
此解决方案似乎对我有用:也保留要从资产目录中的子目录访问的每个文件的副本。使用相同的约定。IElist("subdirectory")
It doesn't read the files in assets folder, but reads all "copied" files in its sub directory. Weird!! But works!
它不读取资产文件夹中的文件,而是读取其子目录中的所有“复制”文件。奇怪的!!但是有效!
回答by 32kda
I use the following two methods to ensure given asset is present:
我使用以下两种方法来确保给定资产存在:
protected boolean hasAsset(String id) {
return hasAsset(id,"");
}
protected boolean hasAsset(String id, String dir) {
int idx = id.indexOf('/');
if (idx > 0 && idx < id.length() - 1) {
if (dir.length() > 0) {
dir = dir + "/" + id.substring(0,idx);
} else
dir = id.substring(0,idx);
return hasAsset(id.substring(idx + 1), dir);
}
try {
return Arrays.asList(context.getAssets().list(dir)).contains(id);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
回答by Richard Sharpe
For some reason, empty directories are not listed.
出于某种原因,未列出空目录。
At least, in my experience, they are not listed.
至少,根据我的经验,它们没有被列出。
That is, if you have some-dir/d1/f1 and some-dir/d2 in the assets area, and you list("some-dir"), d2 is not listed.
也就是说,如果您在资产区域中有 some-dir/d1/f1 和 some-dir/d2,并且您列出了(“some-dir”),则不会列出 d2。