Trouble with reading file from assets folder in Android

前端 未结 3 1094
旧巷少年郎
旧巷少年郎 2020-12-11 11:00

This question is with regards to this one. Since it is a specific question I moved that question by itself here. I have tried creating a text file, \"foo.txt\", an read it i

相关标签:
3条回答
  • 2020-12-11 11:39

    You do not access assets/ at runtime using File. You access assets/ at runtime using AssetManager, which you can get via getResources().getAssets().

    0 讨论(0)
  • 2020-12-11 11:47

    I'm with CommonsWare in that case (that's the safe side :) ), but it should be:

    AssetManager assetManager = getResources().getAssets();
    InputStream inputStream = null;
    
        try {
            inputStream = assetManager.open("foo.txt");
                if ( inputStream != null)
                    Log.d(TAG, "It worked!");
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    Do not use InputStream is = assetManager.open("assets/foo.txt");

    0 讨论(0)
  • 2020-12-11 11:51

    try this :

        private AssetManager am;
         am=getAssets();
    
         InputStream inputStream = null ;  
            try   
            {  
                inputStream = am.open("read.txt");  
            }   
            catch (IOException e) {}  
    
    0 讨论(0)
提交回复
热议问题