open a file outside of assets folder by assetManager

1.1k views Asked by At

I have a binary file in root of apk and I can check it by:

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

It exists in returning list. But how can I open it?
Because when I try to open the file by using this code:

assets.open("/test.bin")

I faced by FileNotFoundException.

1

There are 1 answers

0
hich9n On BEST ANSWER

finally, I soloved it by using getClass().getResourceAsStream() instead of 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;
}