1

I'm a beginning developer for Android and running into the following problem when trying to access an XML file on the SD card.

To start, I performed the following checks: - Permission to read/write external storage is given in android manifest - The file exists in the specified location - I checked: fileexists = true; canread = true; isfile = true - externalstorage state = mounted

Still the FileInputStream gives me the FileNoteFoundException. The code:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}
    //probeersel
    public void myClickHandler(View view) {
        switch (view.getId()) {
            case R.id.button1:

                XPath xpath = XPathFactory.newInstance().newXPath();

                File mealsource = new File(getExternalFilesDir(null)
                         + "/test.xml");

                Toast.makeText(this, "External Files Dir: " + getExternalFilesDir(null), Toast.LENGTH_LONG).show();
                Toast.makeText(this, "Exists: " + String.valueOf(mealsource.exists()), Toast.LENGTH_LONG).show();
                Toast.makeText(this, "Can read: " + String.valueOf(mealsource.canRead()), Toast.LENGTH_LONG).show();
                Toast.makeText(this, "Is file: " + String.valueOf(mealsource.isFile()), Toast.LENGTH_LONG).show();
                Toast.makeText(this, "External Storage State: " + Environment.getExternalStorageState(), Toast.LENGTH_LONG).show();
                Toast.makeText(this, "Absolute path " + mealsource.getAbsolutePath(), Toast.LENGTH_LONG).show();

                FileInputStream mealstream = new FileInputStream(mealsource);

                InputSource inputsource = new InputSource(mealstream);

                expression = "Meals/Meal/ShrtDesc/text()";

                meallist = (NodeList) xpath.evaluate(expression, inputsource, XPathConstants.NODE);

etc

So to recap: All my checks say the file is there to be read and ready for action, but the FileInputStream constructor seems blind to it. What am I to do? What have I missed? Could it have to do with the content of the xml somehow?

Thanks a lot for any help or pointers you can give me.

Paul Siersma
  • 2,036
  • 1
  • 22
  • 25
  • I don't think this will solve your problem but you should use another `File` constructor overload : `new File(getExternalFilesDir(null), "test.xml");` to avoid problems with slashes. – Dalmas Nov 20 '11 at 11:50
  • Thanks @LadaRaider, but as you say it does not solve my problem. A thought: I'm developing in Eclipse and testing the app on my Nexus S. The xml file is on there. However, Eclipse gives me the exception as soon as I've written the FileInputStream line. It obviously cannot know whether the file is there if my phone is not connected to my pc. I'm confused. Shouldn't the code check for the file when the code runs and not before? – Paul Siersma Nov 21 '11 at 08:34
  • Did you try to use another path (something like `new File(Environment.getExternalStorageDirectory(), "test.xml"`) instead of calling getExternalFilesDir(), to see if it's not a problem with this function ? There's a known bug about it, maybe not related but else I don't understand : http://groups.google.com/group/android-developers/browse_thread/thread/b68d40b1f13e12df – Dalmas Nov 21 '11 at 10:51
  • @LadaRaider Thanks again, but this is not what's happening as the xml file persists on my sd card even when I reinstall the apk. I found a similar problem here on SO but I can't properly interpret the answer: "The problem was a directory was being passed in (as well as many files) – Ankur Jun 16 '09 at 7:08". Do you understand what he means? The question is here: [link](http://stackoverflow.com/questions/999771/get-filenotfoundexception-when-initialising-fileinputstream-with-file-object). Thanks again for looking into this for me. – Paul Siersma Nov 21 '11 at 13:06
  • I don't think this is the same issue, because your call to `isFile()` returns true. He probably means that he used a directory path (with many files inside it) instead of a file. Can you give more details about your Exception? In your link, there's an additional info `(Access denied)`. Is it the same for you? – Dalmas Nov 21 '11 at 13:20
  • @LadaRaider I'm not sure how to coax Eclipse out of the error information you ask for. Strangest thing is: Eclipse still gives me the error message but if I ignore it (throws) then my code seems to be working and parsing the xml file! Weird right? Thanks for your help, I'm willing to do more checks if you like but would honestly rather continue working on my app. Again, thanks! – Paul Siersma Nov 21 '11 at 19:04
  • Did you ever find a solution? I'm facing something similar... – robguinness Jul 29 '13 at 09:18
  • @robguinness No afraid not, good luck to you! – Paul Siersma Oct 30 '13 at 13:07

0 Answers0