zayar
zayar

Reputation: 431

Android File Path NOT Found

I trying to do XML parsing program. I also used FileInputStream for my XML file. I placed XML file under android's assets folder,META-INF folder. That's file name is "container.XML".

Here is my code parseXML,

 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 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 got only error message. Hope help!

Upvotes: 3

Views: 3812

Answers (1)

naikus
naikus

Reputation: 24472

I'm assuming you have placed your container.xml file in your application's assets directory inside the apk package.

To open files inside the /assets directory of your android app, you need an AssetManager. The getAssets() is available on the Context object and hence available to your Activity or Service.

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

Upvotes: 3

Related Questions