Android file path not found

I am trying to parse XML. I also used FileInputStream for my XML file. I put the XML file in the Android resources folder, the META-INF folder. This is the name of the file "container.XML".

Here is my parseXML code,

 public void parseXMLinfoBook() throws FileNotFoundException, ParserConfigurationException, SAXException{

        FileInputStream in = new FileInputStream("file:///android_asset/META-INF/container.xml");

        StringBuffer inLine = new StringBuffer();
        InputStreamReader isr = new InputStreamReader(in);

        BufferedReader inRd = new BufferedReader(isr);

        SAXParserFactory spf=SAXParserFactory.newInstance();
        SAXParser spr=spf.newSAXParser();
        XMLReader xmlreader = spr.getXMLReader();

        XmlHandler xmlhe=new XmlHandler();
        xmlreader.setContentHandler(xmlhe);

        }

Here is the Button.SetonClick code,

public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    parseXMLinfoBook();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    tv.setText("ErrorPath "+e.getMessage());
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

I only got an error message. Hope to help!

+3
source share
1 answer

I assume that you put the container.xml file in the application resource directory inside the apk package.

/assets Android, AssetManager. getAssets() Context , , .

AssetManager mgr = getContext().getAssets();
InputStream in = mgr.open("META-INF/container.xml");
InputStreamReader isr = new InputStreamReader(in);
//... Rest of the code
+3

Source: https://habr.com/ru/post/1757968/


All Articles