我的 Android 应用程序在 Assets 目录中有一些文件,我想在启动时通过列出目录中的文件并打开每个文件来打开这些文件。我正在尝试使用 AssetManager 来执行此操作,但它似乎并不像我期望的那样。我的示例代码如下。这是正确的方法还是有更好的方法?
我正在使用以下方法打印出 Assets 目录树。
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);
}
}
在我的 Activity 的 onCreate 方法中,我执行以下操作:
final AssetManager mgr = getAssets();
displayFiles(mgr, "/assets");
displayFiles(mgr, "./assets");
displayFiles(mgr, "/");
displayFiles(mgr, "./");
这给了我以下输出
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
提前致谢!
约翰
最佳答案
呃。问题出在 displayFiles 方法中,它缺少目录和文件名之间的分隔符“/”。对不起,如果我浪费了任何人的时间。下面是 displayFiles 的更正版本。
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 - 如何从我的 Android 应用程序中获取资源的目录列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1495585/