Seth Nelson
Seth Nelson

Reputation: 2608

fileinputstream throwing filenotfoundexception?

I'm trying to open up an xml file to parse it, but I am getting stopped trying to open the file. My code is:

        try {
            return new FileInputStream(location);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }   

where location is a String passed into the function. Upon running my code, I get:

java.io.FileNotFoundException: /C:/Users/Seth/AndroidWorkspace/UCoupon/res/xml/information.xml (No such file or directory)

I gave the full file path, and I havn't mispelled anything. What am I doing wrong?

Upvotes: 1

Views: 1371

Answers (3)

Francesco Laurita
Francesco Laurita

Reputation: 23552

You are trying to load a file using the real path on your PC. Emulator knows nothing about your file system.

When you put an xml into the project folder res/xml/ you can parse it using the standar resource lookup way:

XmlResourceParser xpp = res.getXml(R.xml.myxml); where myxml is the name of your xml file without the extension

Upvotes: 1

bigstones
bigstones

Reputation: 15267

you can't read files on your pc from your device or emulator.

see here for an introduction to Data Storage and here for Accessing Resources (that is probably what you're interested in, as you're trying to access res/xml/)

Upvotes: 2

mreichelt
mreichelt

Reputation: 12455

You can not access files from your host system (which seems to be Windows in your case). Copy the file to your devices/emulators SD card and read it from there - and don't forget to use the WRITE_EXTERNAL_STORAGE permission.

Upvotes: 0

Related Questions