我在 apk 的根目录中有一个二进制文件,我可以通过以下方式检查它:
AssetManager assets = Context().getResources().getAssets();
assets.list("/")
它存在于返回列表中。但是怎么才能打开呢?
因为当我尝试使用此代码打开文件时:
assets.open("/test.bin")
我遇到了 FileNotFoundException。
最佳答案
最后,我通过使用 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;
}
关于android - 通过 assetManager 打开 assets 文件夹外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081749/