【问题标题】:Why does AssetManager list() not show my assets folder?为什么 AssetManager list() 不显示我的资产文件夹?
【发布时间】:2013-07-16 08:13:04
【问题描述】:

在我的 Android 应用程序中,我在 /assets 文件夹中打包了一个需要复制到 SDCARD 的文件。当我将 assets 文件夹的内容列出到 String[] 时,我得到了“images”、“sounds”、“webkit”和“kioskmode”,但我的文件没有手动添加到 assets 文件夹中。

我的代码在这里:

private void copyAsset () {
    AssetManager am = getApplicationContext().getAssets();
    String[] files = null;
    try {
      files = am.list("");
    } catch (IOException e) {

    }

    for (String filename : files) {
        if (filename.equals("images") || filename.equals("kioskmode") ||
                filename.equals("sounds") || filename.equals("webkit")) {
            Log.i(TAG, "Skipping folder " + filename);
            continue;
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            in = am.open(filename);
            File outFile = new File(Environment.getExternalStorageDirectory().getPath(), filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Error copying asset ", e);
        }
    }
}

这是我的应用程序中的第二个类并在我的 MainActivity 中使用 Intent showHelper = new Intent(this, HelperManager.class); startActivity(showHelper); 调用,这有什么不同吗?

我已尝试使用和不使用 getApplicationContext() 位的第二行 (AssetManager am = ...),尝试将文件移动到 /assets 的子文件夹中,并尝试使用前导和尾随斜杠 files = am.list("")。如果我使用子文件夹,则代码运行时文件数组为空(在 files = am.list(""); 行上设置断点并在运行时检查它。 奇怪的是它曾经工作过 - 当我第一次编写代码时,但为了进一步测试,我从手机上的 /sdcard 文件夹中删除了该文件,并且它从未工作过,即使该文件仍在 assets 文件夹中。 如果这很重要,我正在使用 Android Studio。 谢谢

【问题讨论】:

  • 资产文件夹里有东西吗?
  • 是的,我这样做了@blackbelt,如果我浏览到项目位置,我可以在 Android Studio 的项目布局和 Windows 资源管理器中看到它。也许我会尝试删除并重新添加该文件(它是一个 .apk 顺便说一句)。
  • 如果文件与 apk 捆绑在一起,你就很好。您了解continue 关键字的含义吗?是否要将“图像”、“声音”、“webkit”和“kioskmode”写入 SdCard 中?
  • 我已经从资产中删除/删除了嵌入文件,重建它,将其复制回来,并将文件重命名为全小写字符,但仍然没有乐趣。
  • 不@blackbelt,提到的四个文件夹实际上不在资产文件夹中,我不希望它们(因此如果找到它们,上面的代码将跳过复制)。似乎我的 getAssets() 正在查看其他资产文件夹,例如当我运行应用程序时没有包含我的,并且它包括其他资产文件夹,可能是系统或其他库资产文件夹..?

标签: android android-studio assets


【解决方案1】:

设法使用Load a simple text file in Android Studio 作为修复来获得解决方案。它仍然将 4 个文件夹放在 files 数组中,但我可以使用上面给出的代码跳过它们,尽管我宁愿检查我想要的文件而不是我不想要的 4 个!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多