【问题标题】:open a file outside of assets folder by assetManager通过assetManager打开assets文件夹外的文件
【发布时间】:2018-02-15 07:55:28
【问题描述】:

我在 apk 的根目录中有一个二进制文件,我可以通过以下方式检查它:

AssetManager assets = Context().getResources().getAssets();
assets.list("/")

它存在于返回列表中。但是怎么打开呢?
因为当我尝试使用此代码打开文件时:

assets.open("/test.bin")

我遇到了 FileNotFoundException。

【问题讨论】:

  • 你应该把文件放在 assets 目录下。不在根目录下,不管那是什么。
  • @greenapps 文件原本在一个依赖(外部jar)上,编译后出现在apk根目录下。

标签: android android-assetmanager


【解决方案1】:

最后,我使用 getClass().getResourceAsStream() 代替了assetManager:

public byte[] loadBinAsset(String name) {
    AssetManager assets = context.getResources().getAssets();
    InputStream stream = null;
    try {
        try {
            stream = assets.open(name);
        } catch (IOException e) {
            stream = context.getClass().getResourceAsStream("/" + name);
        }
        return readFully(stream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多