Read xml file from Android Asset folder

marcoqf73 picture marcoqf73 · May 11, 2012 · Viewed 23k times · Source

I'm trying to read the same file "xmlfile.xml" from the assets folder and also another copy from the SD card sdcard/download/.

I can Read from SD Card:

  • unfile Return True
  • Esite Return True

I can't not Read from Assets folder:

  • unfile Return False
  • Esite Return False

This code il NOT Working

        File source = new File("file:///android_asset/xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This code il Working

        File source = new File("/sdcard/" + "download" + "/" + "xmlfile.xml");
        boolean unfile = source.isFile();
        boolean Esiste = source.exists();

        try
        {
          // todo
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

someone can explain me how can I read the file from the Assets folder.

thanks marco

Answer

Rawkode picture Rawkode · May 11, 2012

To open an asset you'd need the following piece of code:

InputStream is = getAssets().open("xmlfile.xml")