我怎样才能从我的Android应用程序获取资源的目录列表? [英] How can I get a directory listing of resources from my Android app?
问题描述
我的Android应用程序中的资产目录中的一些文件,我想通过列出该目录中的文件,并打开每个打开的启动。我试图用AssetManager要做到这一点,但它似乎并没有这样做,因为我期望的那样。我的示例code是如下。这是正确的做法还是有更好的方法来做到这一点?
My Android application has some files in the assets directory that I want to open on startup by listing the files in the directory and opening each. I am trying to use the AssetManager to do this but it does not seem to do as I would expect. My sample code is below. Is this the correct way or is there a better way to do this?
和我使用下面的方法来打印出资产的目录树。
And I am using the following method to print out the assets directory tree.
void displayFiles (AssetManager mgr, String path) {
try {
String list[] = mgr.list(path);
if (list != null)
for (int i=0; i<list.length; ++i)
{
Log.v("Assets:", path +"/"+ list[i]);
displayFiles(mgr, path + list[i]);
}
} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}
}
从我的活动的onCreate方法,我做到以下几点:
From my Activity's onCreate method I do the following:
final AssetManager mgr = getAssets();
displayFiles(mgr, "/assets");
displayFiles(mgr, "./assets");
displayFiles(mgr, "/");
displayFiles(mgr, "./");
这给了我下面的输出
Which gives me the following output
09-29 20:08:27.843: DEBUG/GFlash(6543): //AndroidManifest.xml
09-29 20:08:27.954: DEBUG/GFlash(6543): //META-INF
09-29 20:08:28.063: DEBUG/GFlash(6543): //assets
09-29 20:08:28.233: DEBUG/GFlash(6543): //classes.dex
09-29 20:08:28.383: DEBUG/GFlash(6543): //com
09-29 20:08:28.533: DEBUG/GFlash(6543): //res
09-29 20:08:28.683: DEBUG/GFlash(6543): //resources.arsc
在此先感谢!
Thanks in advance!
约翰·
推荐答案
唉。的问题是在displayFiles方法,它缺少隔板,/,目录和文件名之间。很抱歉,如果我浪费任何人的时间。 displayFiles的修正版本如下。
Ugh. The problem was in the displayFiles method, it was missing the separator, "/", between the directory and the filename. Sorry if I wasted anyone's time. A corrected version of displayFiles is below.
void displayFiles (AssetManager mgr, String path) {
try {
String list[] = mgr.list(path);
if (list != null)
for (int i=0; i<list.length; ++i)
{
Log.v("Assets:", path +"/"+ list[i]);
displayFiles(mgr, path + "/" + list[i]);
}
} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}
}
约翰·
这篇关于我怎样才能从我的Android应用程序获取资源的目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!