如何查看Android Asset资源?

发布于 2024-10-01 22:58:32 字数 46 浏览 5 评论 0原文

我想检查 /assets/ 文件夹中是否存在文件。 我怎样才能做到呢?请帮忙。

I want to check, whether a file exists or not in the /assets/ folder.
How could I do it? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯离梦残月幽梦 2024-10-08 22:58:32

我向我的应用程序类之一添加了一个辅助方法。我假设;

  1. 应用程序运行时,资产列表不会更改。
  2. List 并不占用内存(我的应用程序中只有 78 个资源)。
  3. 检查列表上的 contains() 比尝试打开文件并处理异常更快(我实际上没有对此进行分析)。
AssetManager am;
列表<字符串>地图列表;

/**
 * 检查资产是否存在。
 *
 * @param 资产名称
 * @return boolean - 如果存在具有该名称的资产,则为 true。
 */
公共布尔 checkIfInAssets(String assetName) {
    if (mapList == null) {
        am = getAssets();
        尝试 {
            mapList = Arrays.asList(am.list(""));
        } catch (IOException e) {
        }
    }
    返回mapList.contains(assetName);
}

I added a helper method to one of my application classes. I'm assuming that;

  1. the list of assets doesn't change while the app is running.
  2. the List<String> isn't a memory hog (only 78 assets in my app).
  3. checking exists() on the List is faster than trying to open a File and handle an exception (I haven't actually profiled this).
AssetManager am;
List<String> mapList;

/**
 * Checks if an asset exists.
 *
 * @param assetName
 * @return boolean - true if there is an asset with that name.
 */
public boolean checkIfInAssets(String assetName) {
    if (mapList == null) {
        am = getAssets();
        try {
            mapList = Arrays.asList(am.list(""));
        } catch (IOException e) {
        }
    }
    return mapList.contains(assetName);
}
纵情客 2024-10-08 22:58:32

您也可以尝试打开流,如果失败,则文件不存在,如果没有失败,文件应该存在:

/**
 * Check if an asset exists. This will fail if the asset has a size < 1 byte.
 * @param context
 * @param path
 * @return TRUE if the asset exists and FALSE otherwise
 */
public static boolean assetExists(Context context, String path) {
    boolean bAssetOk = false;
    try {
        InputStream stream = context.getAssets().open(ASSET_BASE_PATH + path);
        stream.close();
        bAssetOk = true;
    } catch (FileNotFoundException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    } catch (IOException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    }
    return bAssetOk;
}

You could also just try to open the stream, if it fails the file is not there and if it does not fail the file should be there:

/**
 * Check if an asset exists. This will fail if the asset has a size < 1 byte.
 * @param context
 * @param path
 * @return TRUE if the asset exists and FALSE otherwise
 */
public static boolean assetExists(Context context, String path) {
    boolean bAssetOk = false;
    try {
        InputStream stream = context.getAssets().open(ASSET_BASE_PATH + path);
        stream.close();
        bAssetOk = true;
    } catch (FileNotFoundException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    } catch (IOException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    }
    return bAssetOk;
}
花开雨落又逢春i 2024-10-08 22:58:32

您必须自己进行检查。据我所知,这项工作没有方法。

You have to perform your own checks. As I know, there is no method for this job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文