Check for file existence in androids assets folder?

前端 未结 6 1249
不思量自难忘°
不思量自难忘° 2020-12-30 19:33

My app has .txt files in subdirectories in the assets folder. It reads those .txt files and puts them in a textview. It\'s working great and no problems.

Should I be

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 19:56

    Ideally after apk is built, nobody can remove any assets from it, but if someone decompiled it and recompiles than it may be possible.

    Though for other scenarios also when an asset is not present in apk at Runtime, we can check the existence of asset.

    In our app, we have a provision to build app using gradle, ant and eclipse, and for each build mechanism some of our assets file are bundled in apk and some are not, so to identify if any asset file is present in current build apk at runtime,

    we do this as follows:

    private boolean isAssetExists(String pathInAssetsDir){
        AssetManager assetManager = AppContext.get().getResources().getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open(pathInAssetsDir);
            if(null != inputStream ) {
               return true;
            }
        }  catch(IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }  
        return false;
    }
    

提交回复
热议问题