androidandroid-assetmanager

open a file outside of assets folder by assetManager


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.


Solution

  • 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;
    }